Add active class to a menu link with WordPress

admin

Administrator
Staff member
I am using the WordPress function <strong>wp_nav_menu</strong> to create my main menu.

In this menu, I have a link named "News" for exemple and I would like to make this link highlight when I am in the single.php file template.

At the moment, my code is the one below. I am not "used" to work with this WordPress function.

Code:
&lt;?php wp_nav_menu( array( 'theme_location' =&gt; 'primary', 'menu_class' =&gt; 'nav-menu' ) ); ?&gt;

Is there any way to said for exemple

Code:
if($is_single())
{
echo 'class="active"';
}

I have checked arround on SO and I founded some informations that might be interesting but nothing is really answering my question.

On this page, some one suggest to check in "Show on screen" options : <a href="https://stackoverflow.com/questions...o-dropdown-menu-inside-dynamic-wordpress-menu">add class to dropdown menu inside dynamic wordpress menu</a>

<img src=" " alt="enter image description here">

I also found this page in the codex, but I'm not sure how to use it : <a href="http://codex.wordpress.org/Function_Reference/wp_nav_menu#How_to_add_a_parent_class_for_menu_item" rel="nofollow noreferrer">http://codex.wordpress.org/Function_Reference/wp_nav_menu#How_to_add_a_parent_class_for_menu_item</a>

Thx in advance for the help!