WordPress: Post Classes for Backward Compatibility

If you haven’t checked on the page on migrating plugins and themes to WordPress 2.7, WordPress 2.7 includes a new function for post classes, which allows you to take advantage of the sticky post feature, called post_class().

Now if you were to follow what is shown on the page, which is:

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

You might end up getting something like this if you were to install your theme on WordPress 2.6 and below:

Fatal error: Call to undefined function post_class() in .../index.php on line x 

That’s not good.

One way to workaround this is to create a custom function in your functions.php of your theme:

function mytheme_post_class() {
// if WordPress 2.7+, run built-in post_class()
if ( function_exists(’post_class’) ) return post_class();
// else manually assign some class to the post element
else echo ‘class=”post”‘;
}

And replace the previous code with:

<div id="post-<?php the_ID(); ?>" <?php mytheme_post_class(); ?>>

Hope this helps!

2 Comments
  1. /mek says:

    Thanks! Got my pages working with your advice!

    • zy says:

      No problem :)