is_tax() conditional tag not working
-
18-04-2021 - |
Pregunta
I have a menu item where a CSS class is added with a 'page_active' class (which visually shows an underline under the relevant item). This is added on certain menu items if the is_page('Latest')
or is_archive('sites)
conditions return true.
One of my main menu items (menu-item-3) is a custom taxonomy so instead of using either of the above two methods, I'm using is_tax('web')
for a custom taxonomy called 'web'. This isn't adding the 'page_active' class though and also the CSS class is still being added to one of the other two items.
Below is the code - any ideas about how to solve this would be hugely appreciated.
CSS
.underline.page_active:after {
content: '';
position: absolute;
width: 100%;
height: 5px;
background: #a882ff;
left: 0;
bottom: -0.39em;
z-index: -1;
}
.menu-item-2.underline.page_active:after {
width: 3.1rem;
left: 5.7rem;
z-index: -1;
bottom: 2rem;
}
HTML / PHP
<ul class="nav-menu-items">
<li class="menu-item menu-item-1"><a class="td nav-link underline <?php if (is_page('Latest')) {echo "page_active";};?>" title="Latest" href="<?php echo esc_url(home_url( '/' ));?>">Latest</a></li>
<li class="menu-item menu-item-2 underline <?php if (is_archive('sites') || is_tax('graphic-design')){echo "page_active";}; ?>">Design
<ul class="submenu design-submenu">
<li class="submenu-item submenu-item-1"><a title="Sites We Like" class="td nav-link" href="<?php echo esc_url(home_url('/sites'));?>">Sites We Like</a></li>
<li class="submenu-item submenu-item-2"><a title="Graphic Design" class="td nav-link" href="<?php echo esc_url(home_url('/news_categories/graphic-design'));?>">Graphic Design</a></li>
</ul>
</li>
<li class="menu-item menu-item-3"><a title="Web" class="td nav-link underline <?php if (is_tax('web')){echo "page_active";};?>" href="<?php echo esc_url(home_url('/news_categories/web'));?>">Web</a></li>
<li class="menu-item menu-item-4"><a title="Marketing" class="td nav-link underline" href="<?php echo esc_url(home_url('/news_categories/marketing'));?>">Marketing</a></li>
</ul>
Solución
I found the solution to this, the is_tax();
method needs two arguments - the taxonomy and the term itself, so it should have been:
is_tax('news_categories', 'web')
Just as an extra point the is_archive('sites')
should also have been is_post_type_archive('sites')