最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Add A CSS Class To A Menu Item When A Custom Taxonomy Term is Present On Single Post

programmeradmin1浏览0评论

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, or is_post_type_archive() for a certain post type.

In the code below how would I add a CSS .page-active 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.

发布评论

评论列表(0)

  1. 暂无评论