3

How would I use the same template file for, say three specific taxonomy terms but not the remaining three?

I know taxonomy-taxname.php is the file for all terms

I know taxonomy-taxname-termname.php is the file for a specific term.

But let's say I want to have...

--taxonomy-fruit-redfruit.php for strawberries, tomatoes and raspberries

--taxonomy-fruit-greenfruit.php for watermelons, apples and kiwi

... without resorting to using duplicated individual theme files for each term?

  • Is the design of the templates for 6 the terms very different or is it just a matter of some content being different...? – jsmod Mar 18 at 13:20
  • A bit different. One group of three similar, another group of three similar - the two groups slightly different from each other. – Robert Andrews Mar 18 at 21:00
4

You prepare templates (staying with your example):

  • content-redfruits.php,
  • content-greenfruits.php,
  • content-fruits.php.

Then in taxonomy file taxonomy-fruit.php (copy and rename taxonomy.php or index.php), before main loop check the term slug of the currently-queried object.

$red_templ = ['strawberries', 'tomatoes', 'raspberries']; // term slugs
$green_templ = ['watermelons', 'apples', 'kiwi'];         // term slugs
$template = 'fruits';

$obj = get_queried_object();
if ( $obj instanceof WP_Term ){
    if ( in_array($obj->slug, $red_templ) )
        $template = 'redfruits';
    else if ( in_array($obj->slug, $green_templ) )
        $template = 'greenfruits';
}
while ( have_posts() ) : the_post();

    get_template_part( 'content', $template );

endwhile;

Edit: (answer to the comment)

The above example can be written using is_tax:

$red_templ = ['strawberries', 'tomatoes', 'raspberries']; // term slugs
$green_templ = ['watermelons', 'apples', 'kiwi'];         // term slugs
$template = 'fruits';

if ( is_tax('fruit', $red_templ) )        // check taxonomy, then check terms
    $template = 'redfruits';
else if ( is_tax('fruit', $green_templ) ) // check taxonomy, then check terms
    $template = 'greenfruits';

while ( have_posts() ) : the_post();

    get_template_part( 'content', $template );

endwhile;

is_tax performs some additional comparisons, but here is no significant difference in performance.
Each time you call is_tax, you check if given taxonomy matches the current one (this has already been checked when choosing a template file) and compare terms by id, slug and name.

You do not want to use has_term in this case. This function works on a post, therefore for empty terms it will return falsely false.

  • So, essentially, use a single taxonomy-fruits.php, and then conditionally change some of the content using one of several partial templates, based on which term was accessed? I assume content-fruits.php would be the default for all non-specified terms? – Robert Andrews Mar 18 at 21:06
  • @RobertAndrews Exactly – nmr Mar 18 at 21:33
  • @nmr, I was learning about doing the same as the original poster for different category pages that I have, so I am wondering why your code looks different than the examples on the codex with is_tax() and has_term() and so on. So just for clarification, please, is your answer an example application of conditional tags in a template file, or is it another approach? Edit: If it is different, which method is better and why? – jsmod Mar 21 at 11:46
  • 1
    @jsmod I edited the answer. I used get_queried_object because I don't like to check the same thing unnecessarily several times. – nmr Mar 21 at 22:02

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.