ok so i have a nav menu with the top level of menu items being static pages. When you highlight or visit one of them there is a sub-menu that pops up. Each one of them has there own sub-menu and each sub-menu is full of custom taxonomy terms. Since WordPress will only give the current-menu-item class on an archive page i need to either add my own class or just add some custom css to show and highlight the term parent when viewing a single item.

My problem is I had the code working but then it quit working. See the code below

$has_news = has_term('news');
if ($has_news)
    echo '<style type=text/css>#navbar{display:block}';
    echo '';

This is related to one of my other questions but I figured it would best be suite under its own with different tags.

Related Question: Highlight nav menu terms

wpversion: 3.1.2

1 Answer 1


Problem is that you'd need to pass a $post->ID in order to not return false.

Better use is_object_in_taxonomy($object_type, $taxonomy) and fill both the term and the tax in the function. It returns (boolean), so simply add your class based on the result:

echo $class = is_object_in_taxonomy( 'news', 'your_taxonomy' ) ? 'current-whatever' : '';

// or: (simplified for readabilities sake:

// set empty and override only in case.
// So you avoid dropping errors if the condition was not met and $current not set.
$class = '';
if ( is_object_in_taxonomy( 'news', 'your_taxonomy' ) )
    echo $class = 'current-whatever';
  • what is the ? and what do i put in the : 'HERE'; Commented May 26, 2011 at 16:28
  • Short for if/else. Google for "short hand php". It simply means: "The variable class get's filled: If the condition "object is in the taxonomy" (?=matches condition:) add 'current-whatever' to the var (:=else) add '' empty to it;"
    – kaiser
    Commented May 26, 2011 at 16:31
  • Ok i did not know there was short hand php Commented May 26, 2011 at 16:35
  • Only makes sense for short one liners. Else you can't read your code in a month anymore. Also avoid using this for questioning two different things, before setting a value.
    – kaiser
    Commented May 26, 2011 at 16:37

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.