Adding Custom Classes into WordPress Menu

Normally custom classes can be added into WP navigation menus from admin panel. But sometimes you need to add classes on conditional basis. In this scenario, you cannot add classes from WP admin panel.

For instance, in a project you are required to highlight the two menu items for two different users. You cannot do so from WordPress Dashboard.

In order to resolve this issue, the following code should be added in your Theme’s functions.php file:

This code will check the page title and user status (suppose – a student or teacher). If the set criteria match the mentioned condition, then highlight-menu-item class will be added. You can also check for other $item variables such as:

  1. $item->ID
  2. $item->title
  3. $item->slug

You can also display or hide navigation menu items on conditional basis after some modification in the above mentioned code.

User Comments

Leave a Reply

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

    Get in Touch