
- 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.
I noticed you use the template_redirect function in your plugin to render this code as directory-listing.php rather than, say, naming it single-directory-listing.php and letting WP load it. I was hoping you could explain why and the advantages, etc. Thanks again for sharing this stuff!