Displaying WordPress Custom Post Types Within the Loop

WordPress
Image via Wikipedia

As and example of how to display WordPress custom post types, I will build upon the example that I gave of the directory listing custom post type plugin.

The following code can be added inside of the WordPress loop:

<?php the_content(); ?>

<?php global $post;
	$custom = get_post_custom($post->ID);
	$telephone = $custom["dl10-telephone"][0];
	$email_address = $custom["dl10-email"][0];
	$office = $custom["dl10-office"][0]; 
?>
<?php if($telephone){ echo $telephone; } ?><br />
		
<?php if($email_address){?><a href="mailto:<?php echo $email_address ?>"><?php echo $email_address ?></a><br /><?php } ?>

<?php if($office){ echo $office; } ?><br />
						
						
<?php echo get_the_term_list( $post->ID, 'building', 'Building: ', ', ', '' ); ?> <br />
						
<?php echo get_the_term_list( $post->ID, 'department', 'Department: ', ', ', '' ); ?> 

But what does it mean?

global $post; $custom = get_post_custom($post->ID); pulls the custom meta data that you created through the admin panel for the “post,” which in out case is a custom post type of directory_listing.

Since $telephone = $custom["dl10-telephone"][0]; pulls the meta data that was added in the Directory Listing, all we have to do to display the content is echo it. To keep everything safe from any errors I wrap it in an if statement like if($telephone){ echo $telephone; }. This merely states that if any content has been added for telephone, then display it. The same is true of most of our fields.

Since we registered a couple of our taxonomies, we are going to treat them a little differently. To display our buildings for the custom post type, we will use echo get_the_term_list( $post->ID, 'building', 'Building: ', ', ', '' );. That merely says that for the post that we are displaying (remember that we are in a WordPress loop), we will we will display the custom post type taxonomy that we titled building. We are also separating each term with a comma.

Stay tuned for a post on how to display a full A-Z listing of the employees listed in the custom post type Employee Directory.

Posted in WordPress | Tagged , , , | 1 Comment

WordPress Multisite Theme Development Tip

WordPress stickers & badges
Image by thatcanadiangirl via Flickr

When you are developing a WordPress theme that utilizes the multisite capability, it is sometimes helpful to have consistent content across every site of the network. For instance, you may want to have common navigation so that people can access other sections of the network that are not within the site they are on.

The following code assists in adding the ability to keep common sections of a theme across all sites of a WordPress multisite network. I have found it immensely helpful.

<?php if (is_multisite()) switch_to_blog(1); ?>
<!-- Put the code here that should be controlled by site/blog 1, but appear across the entire site network.  -->
<?php restore_current_blog(); ?>

This code can easily provide capability for things such as global navigation menus across WordPress sites. Please leave any insights on how you have used this capability in the comments.

Posted in WordPress | Tagged , , , , , | Leave a comment

Just a quick note here for those who develop WordPress websites and want to give the end user more control over the website than just content creation:

The folks behind ICanHasCheezburger, Fail Blog and other has released their powerful WordPress custom admin panel. While I have not used it yet, I am excited to get to work using it with my current project.

Check out CheezCap.

Posted on by Joshua | Leave a comment

Preparing for SimTech 2010

Paris Las Vegas in 2009 at night.
Image via Wikipedia

Stamats Integrated Marketing: Technology Conference 2010

When: October 20–22, 2010

Where: Las Vegas, NV

My presentation:

Give Them What They Want: Leveraging Google Analytics for Better Results
Joshua Dodson, Web Developer, Lincoln Memorial University
This session will focus on the advanced areas of the Google Analytics tool including: goals/funnels/values, annotations, custom variables, segmentation, $indexing, campaign tracking, event tracking, custom reports, and more! We will look at ways to use the data we get from these sources to provide us with actionable insights on our projects and campaigns. We will also explore ways of utilizing lesser known features of Google Analytics to present relevant information to website users based on their interests.

Register for the conference and come say “Hi.” I’d love to talk with you about analytics, WordPress, behavioral targeting, a consultation, or lots of other things in Vegas!

Posted in Presentations, Web Analytics | Tagged , , , , , | Leave a comment

Directory Listing WordPress Plugin

In a recent project, I needed the ability to create a custom post type with custom fields for an employee directory. Konstantin wrote an excellent post on how to use custom post types in WordPress. I used his podcast custom post type model as a base and built upon it to create a directory listing plugin. It has turned out very well so far. I have posted the plugin below and will post the templates I use to call the custom fields and display the directory listings in a future post.

Note that this plugin was intended for a directory listing of higher education employees, but can be adapted for other situations.

<?php
/*
Plugin Name: Directory Listing Plugin
Plugin URI: http://www.lmunet.edu
Description: Directory Listing Post Types for WordPress 3.0 and above.
Author: Joshua Dodson
Version: 1.0
Author URI: http://betterwebstrategy.net
*/

class directory_listing10 {
	var $meta_fields = array('dl10-telephone','dl10-email','dl10-office','dl10-position');

	
	function directory_listing10()
	{
		// Register custom post types
		register_post_type('directory_listing', array(
			'labels' => array(
				'name' => __( 'Directory Listings' ),
				'singular_name' => __( 'Directory Listing' ),
				'add_new' => __( 'Add New' ),
				'add_new_item' => __( 'Add New Directory Listing' ),
				'edit' => __( 'Edit' ),
				'edit_item' => __( 'Edit Directory Listing' ),
				'new_item' => __( 'New Directory Listing' ),
				'view' => __( 'View Directory Listing' ),
				'view_item' => __( 'View Directory Listing' ),
				'search_items' => __( 'Search Directory Listings' ),
				'not_found' => __( 'No Directory Listings found' ),
				'not_found_in_trash' => __( 'No Directory Listings found in Trash' ),
				'parent' => __( 'Parent Directory Listing' ),
				),
			'singular_label' => __('Directory Listing'),
			'public' => true,
			'show_ui' => true, // UI in admin panel
			'_builtin' => false, // It's a custom post type, not built in
			'_edit_link' => 'post.php?post=%d',
			'capability_type' => 'post',
			'hierarchical' => false,
			'rewrite' => array("slug" => "directory-listing"), // Permalinks
			'query_var' => "directory_listing", // This goes to the WP_Query schema
			'supports' => array('title','author', 'editor' /*,'custom-fields'*/) // Let's use custom fields for debugging purposes only
		));
		
		add_filter("manage_edit-directory_listing_columns", array(&$this, "edit_columns"));
		add_action("manage_posts_custom_column", array(&$this, "custom_columns"));
		
		// Register custom taxonomy
		register_taxonomy("department", array("directory_listing"), array("hierarchical" => true, "label" => "Departments", "singular_label" => "Department", "rewrite" => true));
		register_taxonomy("building", array("directory_listing"), array("hierarchical" => false, "label" => "Buildings", "singular_label" => "Building", "rewrite" => true));

		// Admin interface init
		add_action("admin_init", array(&$this, "admin_init"));
		add_action("template_redirect", array(&$this, 'template_redirect'));
		
		// Insert post hook
		add_action("wp_insert_post", array(&$this, "wp_insert_post"), 10, 2);
	}
	
	function edit_columns($columns)
	{
		$columns = array(
			"cb" => "<input type=\"checkbox\" />",
			"title" => "Directory Listing Title",
			"dl10_description" => "Description",
			"dl10_position" => "Position",
			"dl10_telephone" => "Telephone",
			"dl10_email" => "Email",
			"dl10_office" => "Office",
			"dl10_departments" => "Departments",
			"dl10_buildings" => "Buildings"
		);
		
		return $columns;
	}
	
	function custom_columns($column)
	{
		global $post;
		switch ($column)
		{
			case "dl10_description":
				the_excerpt();
				break;
			case "dl10_telephone":
				$custom = get_post_custom();
				echo $custom["dl10-telephone"][0];
				break;
			case "dl10_email":
				$custom = get_post_custom();
				echo $custom["dl10-email"][0];
				break;
			case "dl10_office":
				$custom = get_post_custom();
				echo $custom["dl10-office"][0];
				break;
			case "dl10_position":
				$custom = get_post_custom();
				echo $custom["dl10-position"][0];
				break;
			case "dl10_departments":
				$departments = get_the_terms(0, "department");
				$departments_html = array();
				if ($departments) {foreach ($departments as $department)
					array_push($departments_html, '<a href="' . get_term_link($department->slug, "department") . '">' . $department->name . '</a>');
					echo implode($departments_html, ", ");}
				break;
			case "dl10_buildings":
				$buildings = get_the_terms(0, "building");
				$buildings_html = array();
				if ($buildings) { foreach ($buildings as $building)
					array_push($buildings_html, '<a href="' . get_term_link($building->slug, "building") . '">' . $building->name . '</a>');
					echo implode($buildings_html, ", "); }
				break;
		}
	}
	
	// Template selection
	function template_redirect()
	{
		global $wp;
		if ($wp->query_vars["post_type"] == "directory_listing")
		{
			include(STYLESHEETPATH . "/directory-listing.php");
			die();
		}
	} 
	
	// When a post is inserted or updated
	function wp_insert_post($post_id, $post = null)
	{
		if ($post->post_type == "directory_listing")
		{
			// Loop through the POST data
			foreach ($this->meta_fields as $key)
			{
				$value = @$_POST[$key];
				if (empty($value))
				{
					delete_post_meta($post_id, $key);
					continue;
				}

				// If value is a string it should be unique
				if (!is_array($value))
				{
					// Update meta
					if (!update_post_meta($post_id, $key, $value))
					{
						// Or add the meta data
						add_post_meta($post_id, $key, $value);
					}
				}
				else
				{
					// If passed along is an array, we should remove all previous data
					delete_post_meta($post_id, $key);
					
					// Loop through the array adding new values to the post meta as different entries with the same name
					foreach ($value as $entry)
						add_post_meta($post_id, $key, $entry);
				}
			}
		}
	}
	
	function admin_init() 
	{
		// Custom meta boxes for the edit directory_listing screen
		add_meta_box("dl10-meta", "Directory Listing Options", array(&$this, "meta_options"), "directory_listing", "normal", "high");
		add_meta_box("dl10-position", "Position", array(&$this, "meta_position_options"), "directory_listing", "normal", "high");
	}
	
	// Admin post meta contents
	function meta_options()
	{
		global $post;
		$custom = get_post_custom($post->ID);
		$telephone = $custom["dl10-telephone"][0];
		$email_address = $custom["dl10-email"][0];
		$office = $custom["dl10-office"][0];
?>

<p>
  <label>Telephone:</label>
  <br />
  <input name="dl10-telephone" value="<?php echo $telephone; ?>" />
</p>
<p>
  <label>Email Address:</label>
  <br />
  <input name="dl10-email" value="<?php echo $email_address; ?>" />
</p>
<p>
  <label>Office:</label>
  <br />
  <input name="dl10-office" value="<?php echo $office; ?>" />
</p>
<?php
	}


function meta_position_options()
	{
		global $post;
		$custom = get_post_custom($post->ID);
		$position = $custom["dl10-position"][0];
		
?>

<p>
  <label>Position:</label>
  <br />
  <input name="dl10-position" value="<?php echo $position; ?>" />
</p>
<?php
	}

}

// Initiate the plugin
add_action("init", "directory_listing10Init");
function directory_listing10Init() { global $dl10; $dl10 = new directory_listing10(); } ?>

Posted in WordPress | Tagged , , , , , | 2 Comments

Start Gathering Data Now

Simple example graph of Web traffic at Wikiped...
Image via Wikipedia

Some data is better than no data. It is better to begin gather web traffic data as early as you can, but if you have run a website for several years and have not been collecting data, you are still able to transition your website into a web analytics powerhouse.

Web analytics programs such as Google Analytics allow their users to make timely, relevant decisions about the direction of their website. The greatest benefit is the ability to make data-driven decisions instead of opinion-based decisions only.  While everyone has an opinion about the way a website should look, feel, read, etc., it is often based only on personal preference. It may be that the subjective preference of an individual is the wrong method for driving people to convert. Conversion is key.

If you have not yet tapped into the potential of analyzing your web traffic to make timely and relevant decisions, then you should do so now.

  1. Sign up for and install Google Analytics.
  2. Wait.
  3. Wait some more. Allow Google Analytics to gather data.
  4. Analyze the data.
  5. Optimize your site based on the data.

There will be much more to come on this topic. Subscribe to the feed for all updates.

Posted in Web Analytics | Tagged , , , , , , , | Leave a comment

Mind Mapping Your Way to Success

Mind-map showing a wide range of nonhierarchic...
Image via Wikipedia

Mind mapping is an excellent way to generate lots of ideas at one time. Websites with blogs associated with them tend to have more indexed pages than websites that do not have blogs associated with them. It makes complete sense to start a blog to promote your company—you will give readers insight into what your company does and you will have more indexed pages. It is very common to start a blog full-force and then let it fall by the wayside.

One way to ensure that this does not happen is to map out a plan of topics for your blog. I find that mind mapping works best for me, but other similar techniques such as concept mapping or cluster mapping also works very well. Incorporating a mind mapping brainstorming session in your overall web strategy can aid in achieving a better return on interest in your blog. You will have lots of ideas and begin to see connections that may not have been visible otherwise.

Posted in Planning | Tagged , , , , , , | Leave a comment

Hello world!

COMING SOON: Information on how to transform your web presence for better results.

Posted in Miscellaneous | Leave a comment