WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Single post template for custom taxonomies? (8 posts)

  1. yurini
    Member
    Posted 2 years ago #

    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!

  2. LucP
    Member
    Posted 2 years ago #

    I would use template-parts:

    if ( is_tax( 'artikelbredd', 'fullbreddsartikel' ) ) {
    
          get_template_part( 'single', 'fullbreddsartikel' );
    
       }

    It's build in and easy as pie:
    http://codex.wordpress.org/Function_Reference/get_template_part

  3. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with https://codex.wordpress.org/Function_Reference/has_term
    something like this [untested]:

    if(has_term( 'fullbreddsartikel', 'artikelbredd', $post->ID )) {
    //do stuff
    }
    elseif(has_term( 'bloggare1', 'category', $post->ID )) {
    // do stuff
    } else {
    // do stuff
    }

    Because you're on single.php the $post object should be available outside the loop. To check:

    echo $post->ID;
  4. yurini
    Member
    Posted 2 years ago #

    Hi!

    I did not try much with towonders suggestion, it may work.

    At first it did not work with keesiemeijer suggestion either. The problem was that term names are case-sensitive, so type carefully!

    My taxonomys term was "Fullbreddsartikel" with an F instead of an f (even tho the permalink for it was in lowercases). Now it works great with this code:

    <?php
    $post = $wp_query->post;
    	if ( has_term('Fullbreddsartikel', 'artikelbredd', $post->ID ) ) {include(TEMPLATEPATH . '/single-fullbreddsartikel.php');}
    	elseif ( has_term( 'bloggare1', 'category', $post->ID )) {include(TEMPLATEPATH . '/templates/single-bloggare1.php');}
    	else {include(TEMPLATEPATH . '/single-default.php');
    	}
    ?>

    Thank you very much for your help!

  5. Roberto Entringer
    Member
    Posted 2 years ago #

    Hi! look this...

    add_filter('single_template', 'single_template_terms');
    function single_template_terms($template) {
        foreach( (array) wp_get_object_terms(get_the_ID(), get_taxonomies(array('public' => true, '_builtin' => false))) as $term ) {
            if ( file_exists(TEMPLATEPATH . "/single-{$term->slug}.php") )
                return TEMPLATEPATH . "/single-{$term->slug}.php";
        }
        return $template;
    }
  6. studioactiv8
    Member
    Posted 1 year ago #

    @ Robert
    this is Great!! thanks!

    For folks that don't know all the way.
    This is a function that uses your 'taxonomies' and lets you make a single-'taxonomy'.php file. That way you can have different 'single' pages. Add this code to your functions.php file.

    add_filter('single_template', 'single_template_terms');
    function single_template_terms($template) {
        foreach( (array) wp_get_object_terms(get_the_ID(), get_taxonomies(array('public' => true, '_builtin' => false))) as $term ) {
            if ( file_exists(TEMPLATEPATH . "/single-{$term->slug}.php") )
                return TEMPLATEPATH . "/single-{$term->slug}.php";
        }
        return $template;
    }
  7. polenisland
    Member
    Posted 1 year ago #

    Thank you very much for the contribution.
    I Spent a long time to find something, but I could not get it.

  8. longjorn
    Member
    Posted 1 year ago #

    thank you for this filter function!

    If you modify this filter you can use it also to display a single.php for a 'Post Format' like aside or video in a custom single-'post_format'.php

    See my post about this here:

    http://forum.go41.de/topic/singlephp-post-format-template-for-builtin-post-formats

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.