Theming Custom Post Types in WordPress 3.0

So today I developed a simple strategy for dealing with some of the theming issues that arise out of WordPress 3.0 and the new Custom Post Type functionality.

The Issue: You’re working on a site designed long before Custom Post Types were an itch in Matt’s cerebellum. But now that the functionality is there and you want to incorporate it into your theme. Previously you used conditional code to theme your single.php file.

<?php if(in_category('foo')) { ?>
   Do something.
<?php } elseif(in_category('bar')) { ?>
   Do something else.
<?php } else { ?>
   Do yet another thing.
<?php } ?>

The thought of adding yet another layer of conditionals makes you sick to your stomach. Custom Post Types need to be treated completely different. They have different categories, taxonomies, and even different sidbars. Arg!

Solution: One simple function. Call it whatever you want, but here’s what it looks like. When I say simple … I mean simple.

function get_post_in_context() {
global $post;
$type = $post->post_type;
include(TEMPLATEPATH .'/layouts/single-'.$type .'-content.php');
}

Now just create a directory in your theme called “layouts”. Then copy and paste all the markup in your single.php file between the get_header() tag and the get_sidebar() tag to a file named single-post-content.php and save the file to your layouts folder.

Now place a the get_post_in_context() tag into your single.php  where you want the markup from single-post-content.php to show up.

<?php get_header(); ?>
<?php get_post_in_context(); ?>
 <?php get_sidebar(); ?>
<?php get_footer(); ?>

The beauty of this is that you can now create a new single-POSTTYPE-content.php (example:single-events-content.php) file for any of your custom post types and it will automagically get called instead of the single-post-content.php without any more changes to the single.php file.

I can’t take total credit for the idea. Some theme frameworks already employ a version of this strategy. But I need to adapt an existing theme rather than start from a framework. This function above will allow you to scale your existing theme to accomodate new post types without having to redesign your whole site.

Comments

  1. Regena says:

    Hello my friend! I wish to say that this post is awesome, nice written and come with approximately all important infos. I would like to peer extra posts like this .

Trackbacks

  1. [...] has a great overview of custom post types and gives some suggestions for how it can be used.Mike Van Winkle has an article about theming custom post types.WP Storm has an article about the different editor [...]

  2. [...] Mike Van Winkle has an article about theming custom post types. [...]

  3. [...] Mike Van Winkle has an article about theming custom post types. [...]

  4. [...] Mike Van Winkle has an article about theming custom post types. [...]

  5. [...] Mike Van Winkle has an article about theming custom post types. [...]

  6. [...] Mike Van Winkle has an article about theming custom post types. [...]

  7. [...] Mike Van Winkle has an article about theming custom post types. [...]

  8. [...] Mike Van Winkle has an article about theming custom post types. [...]

  9. [...] Mike Van Winkle has an article about theming custom post types. [...]

  10. [...] Custom Post TypeCreating Custom Taxonomies in WordPress 3Introducing WordPress 3 Custom TaxonomiesTheming Custom Post Types in WordPress 3.0Custom Types in WordPress 3 Simplified Our SiteUsing Custom Taxonomies to Create a Movie [...]

  11. [...] Post Type Creating Custom Taxonomies in WordPress 3 Introducing WordPress 3 Custom Taxonomies Theming Custom Post Types in WordPress 3.0 Custom Types in WordPress 3 Simplified Our Site Using Custom Taxonomies to Create a Movie Database [...]

  12. [...] Post Type Creating Custom Taxonomies in WordPress 3 Introducing WordPress 3 Custom Taxonomies Theming Custom Post Types in WordPress 3.0 Custom Types in WordPress 3 Simplified Our Site Using Custom Taxonomies to Create a Movie Database [...]

  13. [...] Mike Van Winkle has an article about theming custom post types. [...]

  14. [...] Mike Van Winkle has an article about theming custom post types. [...]

Speak Your Mind

*

Notify me of followup comments via e-mail. You can also subscribe without commenting.