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
Elegant Themes
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 lovers 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. Alessandro says:

    What’s nav_menu_css_class?

    Nothing happens in my theme…

  2. 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.

  3. 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!

  4. Andrea Ballerino says:

    Thanks for this code, work correctly in my theme!

  5. 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.