0

I have a custom post type - Films. It has a taxonomy of Film_cat, which in turn has two terms/categories - Term1 & Term2.

For each single post within Term1, I would like to display a specific single.php template. Each single post within Term2 should also have it's own single.php template.

I am currently utilising taxonomy-film_cat-term1.php for the taxonomy archives templates, however can't find any info on how to create different single.php templates based on the taxonomy term.

Effectively, I need a single.php template for a Custom Post Type > Taxonomy > Taxonomy Term.

1

You can load a different template for single posts via the single_template filter. Just use the has_term() function to check if the post has a specific term in your custom taxonomy.

function wpa_107626_single_template( $single_template ) {
     // check if the post fits some condition
     if ( has_term( 'term1', 'film_cat' ) ) {
          $single_template = get_stylesheet_directory() . '/single-term1.php';
     }
     return $single_template;
}
add_filter( 'single_template', 'wpa_107626_single_template' ) ;
  • +1. Why no global $post;? – Brad Dalton Mar 1 '14 at 2:00
  • Why, is it not working? The third/last parameter of has_term() is $post and by default is always set to the current post, so I don't think you need to global $post. – helgatheviking Mar 1 '14 at 9:51
  • Works well, just curious about the use of global $post; – Brad Dalton Mar 1 '14 at 11:42
  • Cool. I think it is covered by the 3rd parameter of has_term(). – helgatheviking Mar 1 '14 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.