Support » Developing with WordPress » Custom Single Template for Taxonomy Term

  • Resolved bibliofille

    (@bibliofille)


    I’ve got a site with Custom Post Type called “People.” Within “People” is a custom taxonomy called “People Categories.” Within that taxonomy are several terms, however, I’m trying to specifically target the “Not Just Another Pretty Face Artists” term.

    What I want to do is load a specific single template for People posts that will load when the “Not Just Another Pretty Face Artists” term is selected.

    It will look different and display custom fields (through ACF Pro) than all other single posts, so I’d rather not just use the single-people.php template.

    I created a test post and selected “Not Just Another Pretty Face Artists” as the term. This is the only term selected for this post.

    I added the following to functions.php in order to load a specific single template for this taxonomy term:

    function get_custom_single_template($single_template) {
        global $post;
    
        if ($post->post_type == 'people') {
            $terms = get_the_terms($post->ID, 'not-just-another-pretty-face-artists');
            if($terms && !is_wp_error( $terms )) {
                //Make a foreach because $terms is an array but it supposed to be only one term
                foreach($terms as $term){
                    $single_template = dirname( __FILE__ ) . '/single-not-just-another-pretty-face-artists.php';
                }
            }
         }
         return $single_template;
    }
    
    add_filter( "single_template", "get_custom_single_template" ) ;

    Note: “People” is the slug for the “People Categories” taxonomy.

    Every time, this post loads single-people.php (I’m using the What The File plugin to test) and NOT single-not-just-another-pretty-face-artists.php, which is what I want to load. So there must be an issue with the way I’m trying to filter the single template.

    I’ve looked through similar posts, but haven’t quite found the right help so far. Thanks in advance for reading!

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Single Template for Taxonomy Term’ is closed to new replies.