Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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';
share|improve this answer
    
what is the ? and what do i put in the : 'HERE'; –  xLRDxREVENGEx May 26 '11 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 May 26 '11 at 16:31
    
Ok i did not know there was short hand php –  xLRDxREVENGEx May 26 '11 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 May 26 '11 at 16:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.