Add Category class to body tag of single post template

Picked this up on the Codex:

add_filter('body_class','add_category_to_single'); function add_category_to_single($classes, $class) { if (is_single() ) { global $post; foreach((get_the_category($post->ID)) as $category) { // add category slug to the $classes array $classes[] = $category->category_nicename; } } // return the $classes array return $classes; }

Changing the template for the single post template via category WordPress childtheme

Custom post types are great. But they do add extra tabs on a website. I’ve seen some dashboard disasters with large amounts of CPT tabs. You can actually sidestep CPTs and still have different single posts using different templates per category (without using the Post Template plugin). I picked up this very nifty code here. I needed to modify it because I use child themes and Snilesh was a Parent theme.

First duplicate/rename the single.php files you’re going to need for different templates:




single_default.php //For default single post template  for post other than above categories.

Then open up you main single.php file and delete all the code and paste this in its place:


  $post = $wp_query->post;

  if (in_category('portfolio')) {


  } elseif (in_category('news')) {


  } elseif(in_category('wordpress')) {







And of course note that I used STYLESHEETPATH for a child theme instead of TEMPLATEPATH. What a great way to keep WordPress lightweight and still have stylistic versatility.

Shortcodes inside custom fields

A WordPress developer friend directed me to this post which shows how to parse shortcodes inside of custom fields. This might be really simple but it’s incredibly useful when customizing the content management experience. One more feature to preserve the integrity of design with added functionality.

Just enter your new custom field name. In this case ‘order’. Then paste in your shortcode inside the value field. Of course you must add this snippet to your single.php template so your shortcode shows up:

if ( get_post_meta($post->ID, 'order', true) )
echo do_shortcode(get_post_meta($post->ID, 'order', $single = true));

Custom Post Types and Custom Post Templates


UPDATE (11/28/2011):

Simon Wheatley has upgraded the plugin so that you only need to update the functions.php to get templates to work on CPTs. Function is here. I’m also posting it below for reference:

/** * Hooks the WP cpt_post_types filter * * @param array $post_types An array of post type names that the templates be used by * @return array The array of post type names that the templates be used by **/ function my_cpt_post_types( $post_types ) { $post_types[] = 'movie'; $post_types[] = 'actor'; return $post_types; } add_filter( 'cpt_post_types', 'my_cpt_post_types' );


I’m doing a kind of microsite for a client using custom post types. That site really needs to have its own feel within the larger site. I could have used Multisite but didn’t want to add that layer of admin confusion for the client (but I will suggest it if they continue to do medium to large microsites). I was having some trouble styling adjusting the post type template. It was simple enough to do single-posttype.php however since I’ve got several landing page gateways before the meat of the site I needed to those landing pages to have separate templates. It’s actually not possible to do the obvious to specify a template for one of those landing pages, that being ‘single-ID.php’. You can do this with pages but not with posts. Even though custom post types are theoretically pages WordPress currently sees them as posts (pages are actually just another custom post type so that is probably where the conundrum lies).

I found a really nice solution for doing this in pure code here but after testing it found that it made not-so-nice permalinks because a var needed to be added manually to the links. A larger problem was that adding Addthis share tools to the post would share the original template and not the var in the permalink which means anyone who clicks on the page form Twitter would actually be seeing a page in the original template. So this solution is really only good if you can avoid Share tools on your page.

So my solution was to install the Custom Post Templates plugin and implement this change on line 48:

$this->add_meta_box( ‘select_post_template’, __( ‘Post Template’, ‘custom-post-templates’ ), ‘select_post_template’, ‘post‘, ‘side’, ‘default’ );


$this->add_meta_box( ‘select_post_template’, __( ‘Post Template’, ‘custom-post-templates’ ), ‘select_post_template’, ‘my-custom-post-type‘, ‘side’, ‘default’ );

One other strategy I employed was to rename the plugin folder and name in the WordPress install. This is so that it won’t be overwritten when Simon Wheatley updates the plugin. This is after all just for a microsite in a custom post type. I may want to deploy the plugin on regular posts for the client and leave this version of the plugin in tact.


Now, on that specific custom post type I get options to switch templates. I wanted to keep this away from client client access but it’s a small price to pay. Microsites do allow some liberties to be taken with how they’re built because of their often shorter shelf life from that of the broader site.

Embedding Shortcodes into WordPress Theme Templates

This one is pretty obvious but I only came across it a few times in the forums. It’s really nice to be able to embed plugin functionality right inside of templates. A good example is a Nivo slider. You want your client to be able to update their Nivo image but you may not want to see the shortcode for the Nivo right inside their page post. So the answer is to put that right inside a theme template.

Here’s an example of code I’m using in a template to do exactly that.

< ?php echo do_shortcode('[jj-ngg-jquery-slider gallery="40" width="680" height="421" effect="fade" pauseTime="5000" disablecaptions="1" directionNav="false"]'); ?>

Embedding Blog Feed Inside a Page with Custom Date Formatting

Useful if embedding a typical WordPress formatted blog inside a page. For more advanced Blog embedding use Blog in a Blog plugin.

First put this function in the functions.php:

add_shortcode( 'entry-link-published', 'my_entry_published_link' );

I wanted more detailed date formatting so I dropped this function in as well:

function my_entry_published_link() {

 /* Get the year, month, and day of the current post. */
 /*$year = get_the_time( 'Y' );*/
 $month = get_the_time( 'm' );
 $day = get_the_time( 'd' );
 $year = get_the_time( 'Y' );
 /* Add a link to the monthly archive. */
 $out .= '<div>' . get_the_time( 'M' ) . '</div>';
 /* Add a link to the daily archive. */
 $out .= ' <div>' . $day . '</div>';

 /* Add a link to the yearly archive. */
 $out .= '<div>' . $year . '</div>';

 return $out;

Then I dropped in this in my homepage template file. I’ve probably already hacked away at this example and can’t remember where I picked it up from so it’s formatted specifically for the site I was working on. But I think there’s enough here to manipulate for any use.

$postslist = get_posts('numberposts=2');
foreach ($postslist as $post) :
 <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></h2></a>
 <?php echo my_entry_published_link(); ?>
 <?php /*?><?php twentyten_posted_on(); ?><?php */?>
 <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><span>Read More</span></a>
 </div><!-- end metaHomeThumb -->
 <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail( array("class" => "alignleft post_thumbnail")); } ?></a>
 <?php the_excerpt(); ?>
 <?php endforeach; ?> 

One note. I’m using the echo my_entry_published_link() shortcode for date meta and formatting instead of the WordPress default of twentyten_posted_on() which I’ve left commented out in the above example.

Custom Post Type Archive on Homepage Page Template

Customized code from this page. This is a way to create a page template that acts as a Custom Post Type Archive. This was necessary to have a Custom Post Type Archive as a homepage which I can style in CSS.

<?php $args = array( 'post_type' => 'work', 'posts_per_page' => 10 ); ?>
<?php $loop = new WP_Query( $args ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2><a href="<?php the_permalink(); ?>"
title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ),
the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
<?php the_title(); ?></a></h2>
<?php the_post_thumbnail(); ?>
</div><!-- #post-## -->
<?php endwhile; ?>