Easy Guide to Displaying Custom Post Types in Your WordPress Theme
August 2, 2010
WordPress 3.0 offers the exciting new world of custom post types, giving you the ability to manage and present content with fine-grained control. Creating custom post types is the easy part, but how do you get them to show up in your theme? This tutorial will walk you through this process from start to finish. You’ll be able to incorporate custom post types into your WordPress theme in three easy steps.
Step 1: Create a Custom Post Type
There are basically two ways to create custom post types: add them with your theme’s functions.php file or create them using the help of a plugin, such as Easy Post Types.
For this example, we’ll create a custom post type for recipes. If you wanted to add the post type through your functions file instead of installing another plugin on your site, here is what you would include:
// Creates Recipes post type register_post_type('recipes', array( 'label' => 'Recipes', 'public' => true, 'show_ui' => true, 'capability_type' => 'post', 'hierarchical' => false, 'rewrite' => array('slug' => 'recipes'), 'query_var' => true, 'supports' => array( 'title', 'editor', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'thumbnail', 'author', 'page-attributes',) ) );
Most of the items above are fairly self-explanatory. You will alter the post type, label and rewrite with the type of post you want to create.
If you want more information about registering custom post types, check out the WordPress codex.
After you save your functions file, you should see your new custom post type in the dashboard menu, as shown here. Now you can start adding new recipe posts.
Step 2: Add Your Post Type to a Custom Page Template
Adding your newly created custom post type to its own page is easier than you think. Make a duplicate of your theme’s page.php file. Add this to the top to let WordPress know that it is custom page:
<?php /** * Template Name: Recipes Page * * Selectable from a dropdown menu on the edit page screen. */ ?>
Then you will add this bit right before the loop (above the if ( have_posts() section):
<?php query_posts( 'post_type=recipes'); ?>
Step 3: Create a New Page For Your Custom Post Type
Create a page as usual through the WordPress dashboard. Give it a title and make sure to assign the “recipes” template to your page. Click “Publish” and now you should see a full page of your recipe posts.
If you want to have thumbnails included, make sure to enable post thumbnails for your theme and add this tag into your custom page template:
<?php the_post_thumbnail(); ?>
That’s everything from start to finish. There’s a lot more information to explore in the codex and other tutorials for pimping out your custom post types even further. Now that you have the basic idea, you can customize from there and start managing your content with greater flexibility.
Because this plugin/widget is provided free of charge, I simply ask you to link back to Pressography.com in a post on your blog, or from one of your sites. It doesn't need to be anything fancy - just a note saying that you're using it, and whether or not you like it. However, there is no requirement that you do this, you're free to use the plugin regardless.



Comments
Got something to say?