Add a CSS Class To Menu Item When Custom Taxonomy Term is Present On A Page
-
I have a situation where if I’m on a single post page for a custom post type, if a custom taxonomy term is present I want to highlight the main nav menu related to that taxonomy term.
On archive pages this is fairly straight forward because I can use functions like
is_tax()
for a taxonomy and term, oris_post_type_archive()
for a certain post type.In the code below how would I add a CSS ‘.page-active’ class to a ‘marketing’ nav menu item when the custom term ‘marketing’ is present in the term list for the page. I am bringing the terms onto the page using
get_the_term_list()
The code for the menu-item is:
<li class="menu-item menu-item-4"><a title="Marketing" class="td nav-link underline <?php if (is_tax('news_categories','marketing')){echo "page_active";};?>" href="<?php echo esc_url(home_url('/news_categories/marketing'));?>">Marketing</a></li>
The code for showing the term on the custom post type single page is:
<?php echo get_the_term_list ($post->ID, 'news_categories', ' ', ', ', ''); ?>
Any ideas how to approach this would be amazing. Also because the menu has a very specific design on mobile it has been hard coded so I’m not using the normal WordPress menu functions.
- The topic ‘Add a CSS Class To Menu Item When Custom Taxonomy Term is Present On A Page’ is closed to new replies.