WordPress: create a different single.php template for each post format

Suppose that your theme supports post formats and you want a different single.php template for each format. The solution in WordPress is pretty simple.

Add the following code to your functions.php file:


function single_template_terms($template) {
    foreach( (array) wp_get_object_terms(get_the_ID(), get_taxonomies(array('public' => true, '_builtin' => true))) as $term ) {
        if ( file_exists(TEMPLATEPATH . "/single-{$term->slug}.php") ) {
            return TEMPLATEPATH . "/single-{$term->slug}.php";
        }
    }
    return $template;
}

add_filter('single_template', 'single_template_terms');

Then create a template called single-post-format-yourformat.php, where yourformat can be status, video, aside and so on.

Back to top