0

I am building a site with 2 custom taxonomies (wired-categories, wired-types) assigned to a CPT (wired). I want to show the term in the page title when it is brought up using the taxonomy-wired-categories.php or taxonomy-wired-types.php archive pages.

My header.php file has the following conditionals for creating the title tags on pages according to type:

<title>
<?php 
 if(is_front_page()): bloginfo('name'); ?> &middot; <?php bloginfo('description');
 elseif(is_home()): single_post_title(); ?> &middot; <?php bloginfo('name');
 elseif(is_archive()): ?>Archive &middot; <?php bloginfo('name');
 elseif(is_category()): single_cat_title(); ?> &middot; <?php bloginfo('name');
 elseif(is_404()): ?>404 &middot; <?php bloginfo('name');
 elseif(is_tag()): single_tag_title(); ?> &middot; <?php bloginfo('name');
 elseif(is_date()): ?>Date Archives &middot; <?php bloginfo('name');
 elseif(is_author()): the_author(); ?> &middot; <?php bloginfo('name');

 elseif(is_tax()): single_term_title(); ?> &middot; <?php bloginfo('name'); //this line should be returning an appropriate page title

 else: ?><?php the_title(); ?> &middot; <?php bloginfo('name');
 endif;
?>
</title> 

With the above, when I visit e.g. [domain]/wired-category/[term] I am seeing:

"Archive • [site name]"

when I expect to see:

"[term] • [site name]"

In the browser tab. Why would this be?

1 Answer 1

0

because is_archive is earlier so it doesn't evaluate the others, it's checked from the top down and since is_archive is super generic it will be true for all archives, categories, tags, dates, etc etc. You need to go from the most specific to the least specific, not the other way around.

Think of it like this:

 if(is_food()): echo 'food';
 elseif(is_meat()): echo 'meat';
 elseif(is_chicken()): echo 'chicken';
 else: ....
 endif;

Anything that's food will be true, so it will print food. It will only check is_meat if it's not food, but since is_food is always true when is_meat is true, it will never be checked.

Instead this would work better:

 if(is_chicken()): echo 'chicken';
 elseif(is_meat()): echo 'meat';
 elseif(is_food()): echo 'food';
 else: ....
 endif;

Always go from the most specific/rare/unique first, and the most generic/general last.


Note that it's not ideal to do this in the theme template though, WordPress has hooks and APIs for generating the title tags already, and you should work with this system rather than preventing it from working with hardcoded title tags

1
  • Ooooh thank you! I'll look into the native WP options, but I have found in the past that they generate titles other than what I (or clients) would want, usually. I imagine that's controllable. Will test this ordering approach when I'm back at my desk tomorrow and come back to accept the answer.
    – mtm
    Jul 16, 2023 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.