Single post template for custom taxonomies?
-
I need help to filter out and use single post templates for custom taxonomies.
I use a very simple and powerful filter in single.php to use single post templates for ex. posts in a specific category. I do not get it to work properly for custom taxonomies tho. I have a custom taxonomy called “artikelbredd” and have posts to witch I added the taxonomy term “fullbreddsartikel”. This code does not take effect for posts with the requsted custom taxonomy term:
This is my single.php:
<?php $post = $wp_query->post; if ( is_tax('artikelbredd', 'fullbreddsartikel') ) {include(TEMPLATEPATH . '/single-fullbreddssida.php');} elseif ( has_category('bloggare1') ) {include(TEMPLATEPATH . '/templates/single-bloggare1.php');} else {include(TEMPLATEPATH . '/single-default.php'); } ?>
This is my custom taxonomy code snippet in functions.php:
add_action( 'init', 'build_taxonomies', 0 ); function build_taxonomies() { register_taxonomy( 'placering', 'post', array( 'hierarchical' => true, 'label' => 'Placering och artikelbredd', 'query_var' => true, 'rewrite' => true ) ); register_taxonomy( 'typsnitt', 'post', array( 'hierarchical' => true, 'label' => 'Typsnitt i löpsedeln', 'query_var' => true, 'rewrite' => true ) ); register_taxonomy( 'artikelbredd', 'post', array( 'hierarchical' => true, 'label' => 'Artikelbredd', 'query_var' => true, 'rewrite' => true ) ); }
If I use this code instead in single.php:
<?php $post = $wp_query->post; if ( array( 'taxonomy' => 'artikelbredd', 'terms' => array('fullbreddsartikel'), 'field' => 'slug' ) ) {include(TEMPLATEPATH . '/single-fullbreddssida.php');} elseif ( has_category('bloggare1') ) {include(TEMPLATEPATH . '/templates/single-bloggare1.php');} else {include(TEMPLATEPATH . '/single-default.php'); } ?>
It will use the single post template “single-fullbreddssida.php”, but it wont pass to “single-default.php” to posts without that custom taxonomy asigned to it – the default post or the elseif statement with the template “single-bloggare1.php”. And array filter above cannot be correct either because it uses “single-fullbreddssida.php” to every posts nomatter category or taxonomy value.
Im open to use filter code in functions.php to, but if it is possible to solve via single.php I would prefer that as it is a very simple and flexible way to add multiple filters and works great for tags and categories.
Would be greatful for any help!
- The topic ‘Single post template for custom taxonomies?’ is closed to new replies.