Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

Add a Custom Class in WordPress Menu Item using Conditional Statements

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
Add a Custom Class in WordPress Menu Item using Conditional Statements

In most cases when styling WordPress navigation menus, you can simply add CSS classes from the WordPress admin panel. Recently while working on a project, we found ourselves in a troublesome situation. We wanted to add a custom class to a specific menu item only on single post pages. After looking around for a while, we could not find any solution. Our last resort was to ask on twitter. Otto (@Otto42) replied by saying it is possible by using filters, but there are no documentation for the filter.

After looking in the core for a while, we figured out the solution. What you need to do is paste the following code in your functions.php file:

//Filtering a Class in Navigation Menu Item
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function special_nav_class($classes, $item){
     if(is_single() && $item->title == 'Blog'){
             $classes[] = 'current-menu-item';
     }
     return $classes;
}

The code above is simply checking if it is a single post page, and the menu item title is Blog. If the criteria is matched, then it is adding a class “Current-menu-item”. We needed to add a custom class in order to make it work with this design that we are working on.

If you can’t tell already, basically what we wanted to do was keep the blog item highlighted in the menu when the user was on a single post. This allowed them to see that the single posts are part of the blog. This normally doesn’t make sense, but in the design that we are working on, it did make sense.

If you were desperate looking for this code, we hope that this article helped. You can check for other $item variables also. Some examples are: $item->ID, $item->title, $item->xfn

Quick Edit: After posting this article on twitter, one of our users @dbrabyn pointed out that we could’ve easily accomplished this with CSS Body classes. For example:

.single #navigation .leftmenublog div{display: inline-block !important;}

Basically what we did was added an additional div to display an arrow icon to our menu. This arrow would only be shown if the class was either hovered over, or selected. Otherwise it was set to display: none; By using the body class, we just made the div element display only for the specific menu class.


Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. Annette Voelckner says:

    Thank you so much! After a lot of research this was exactly what I needed.

  2. Ben says:

    Any idea how this could be amended to add a class to the anchor element rather than the list element?

  3. Matt Vaden says:

    Thank you very much for this tip! I know exactly what you mean when you say that for the design you were working on you needed “Blog” to be highlighted in the menu when the user was reading a post. That is exactly what my project needed!

    If reverse (parent) selectors were available in CSS, this wouldn’t be necessary, but that’s neither here nor there, because CSS doesn’t support reverse selectors.

    Thanks again for the tip.

    Regards,
    Matt

  4. ginjoeWP says:

    Very helpful. Thank you!
    I outsourced my menu to a template part called nav-main.php
    The snippet works even when you write it just between $defaults = array(); and wp_nav_menu($defaults); Therefore, leave the functions.php untouched.

  5. Amelie Müller says:

    Wow, thanks so much. I’ve been trying to figure this for a while, you saved me!

  6. Hugo says:

    I was looking a solution for a long time, and now I’ve got it!
    THANKS A LOT

  7. Alessandro says:

    What’s nav_menu_css_class?

    Nothing happens in my theme…

  8. Jayson T Cote says:

    @Syed, I really appreciate all of your tuts and articles, thanks so much for this snippet. I needed to apply the same function to a client site and this saved me plenty of research time.

  9. Patricia Petro says:

    Put the code in my function file . . . added ‘ && in_category ( ‘6’ ) ‘ to highlight menu item for posts in that one category . . . and it works like a dream. Thank you!

  10. Andrea Ballerino says:

    Thanks for this code, work correctly in my theme!

  11. Mattia says:

    Doesn’t WordPress add automatically those classes (current-menu-item, current-post-category, etc.) by default?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.