Back to Top

How to Change Parent Menu Class in WordPress

How to Change Parent Menu Class in WordPress

Hello guys, Today I come with one very small and useful solution of WordPress. WordPress is very popular CMS platform in PHP and provides extremely useful feature is hooks which are very important when you want to customize your WordPress developed functionalities.

Menu navigation is an important part of any site development. Sometimes you want to add a custom class in the output menu in the WordPress as per your project requirement. So, you can change the menu class easily by applying parameter in wp_nav_menu function but it changes the all menu class.What if you have a hierarchical menu? and you only want to apply the class to parent menu then? At that time you need to write the custom code which will apply the class to the parent menu only.

Here I am going to share about to change the menu with the class name of children or submenu. And you know WordPress provides the hook for that also so you can do it very easy manner.

So, Let us check out code and get into the depth

The filter name is wp_nav_menu_objects using that you can find and customize the current menu items.This filter first view at the filter value before calling wp_nav_menu and find information on current menu item such as menu ID, title, class or URL, etc. recorded in a global variable.Once the call the menu using the wp_nav_menu function, it will use settings set using wp_nav_menu_objects filter to display the information.

That’s it.Isn’t it easy and understandable? You just need place filter and function into your theme’s functions.php file and you are ready to go.

This function adds the class to the parent menu if menu contains a submenu but you can modify as you want.This function will check if menu contains a submenu and it is parent menu then it will add the class to this menu.

You can add this function and modify as you want. Like you can add class to the submenu or activate submenu.

Hope this function will be useful to you.Share your comments or suggestions to improve this article or may be another super easy method to achieve this.

Share:
Share on Facebook1

Share on LinkedIn0Tweet about this on TwitterShare on Google+0Buffer this pageDigg thisShare on Reddit0

Leave a Reply

Your email address will not be published. Required fields are marked *

Most Popular Posts

Implementation of Google Prediction Api with Example

Posted on 10 months ago

Ankit Shah

How To Install Bugzilla on Windows

Posted on 4 years ago

Bhumi

How to create Object Copy in PHP

Posted on 4 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 1 year ago

Bhumi

The Reader’s Poll – August 2014

Posted on 2 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 2 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 2 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 3 years ago

Bhumi