has_term if/echo else/echo function

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

Read More
<? 
$has_news = has_term('news');
if ($has_news)
    echo '<style type=text/css>#navbar{display:block}';
else
    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

Related posts

Leave a Reply

1 comment

  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';