WordPress.org

Ready to get started?Download WordPress

Forums

Single post template for taxonomy term? (4 posts)

  1. Paul Whitener Jr.
    Member
    Posted 1 year ago #

    I have a custom taxonomy associated with a custom post type and would like to be able to have a single post template apply to particular terms only.

    For example, let's say the post type is "resources" and I have a taxonomy for "resource types" which contains terms like "whitepaper," "video," etc.

    What I want to be able to do is have a single post template for posts beloning to one of these terms. Using the term "whitepaper" as an example, I would expect something like:

    single-whitepaper.php
    single-resource-types-whitepaper.php

    The function in this post seems like it should work but it's not.

    Does anyone have any thoughts on how this might be set up? Many thanks if so..... Paul

  2. Peter_L
    Member
    Posted 1 year ago #

    single-whitepaper.php should work.

    single-resource-types-whitepaper.php is wrong as it's not a single post but a taxonomy. Try http://codex.wordpress.org/Template_Hierarchy#Custom_Taxonomies_display

  3. Paul Whitener Jr.
    Member
    Posted 1 year ago #

    Hi Peter, it's not working, neither with or without the function I referenced above.

    The link you provided applies to the taxonomy templates, these work as expected but apply to aggregate pages (archive listings of various posts assigned to terms). I'm looking to isolate a post by it's term and apply a custom "single" template.

    The post is inheriting the single template for the post type (using the example above, a whitepaper is pulling single-resources.php) but variations of adding the term slug to the single template are not working.

    This isn't supported by WordPress's template hierarchy out of the box but I'm hoping a function can be put together to do so. I see several posts of folks looking to do the same and am happy to help devise a solution.

  4. Paul Whitener Jr.
    Member
    Posted 1 year ago #

    I did not get this to work but I did get a workaround in place. I was able to set up has_term in my single template to determine whether the current post contained one of the terms I was looking to isolate and work that into a conditional statement to serve up code if so.

    Using my example case above, here's some code that checks if the post contains the term "whitepaper" from my "resource_types" taxonomy:

    <?php if( has_term( 'whitepaper', 'resource_types' ) ) {
    echo 'some code here';
    } else {
    echo 'some other code here';
    } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic