1
votes

I am really new to wordpress. I have a custom menu that I would like to only display specific menu item when the users browser lands on a specific page.

wp_nav_menu(array('theme_location' => 'menu-main'));

Is there a way to have wp_nav_menu() hook to only display specific menu items that have specific css class items?

2
No, there is not. However, research wp_nav_menu - you'll see that you can have multiple menus that you show depending on which page you are on, and further, you can "filter" the menu, so with custom code, you could construct a filter that limits which menu item(s) display in given circumstances.random_user_name

2 Answers

1
votes

If I understand your question well, Yes, there is a way. You can use conditionals. PHP if statements. WordPress allows for you check the page title, ID, category before you execute a command e.g

if( is_page( array( 'about-us', 'contact', 'management' ) ) {

     //either in about us, or contact, or management page is in view
     //execute something

} else {

     //none of the page about us, contact or management is in view
     //execute something

}

And you can add a class/ID to the wp_nav_menu

So joining the two ideas, you can have:

// When Page 42 (ID) is being displayed.
if ( is_page( 42 ) ) {

    wp_nav_menu(array('menu_id' => 'pines', 'menu_class' => 'pnav'));

} elseif( is_page( array( 'about-us', 'contact', 'management' ) ) {

     wp_nav_menu(array('menu_id' => 'bananas', 'menu_class' => 'nav'));

} else {

     wp_nav_menu(array('menu_id' => 'main-id', 'menu_class' => 'main-nav'));

}

Having added unique IDs and classes, you can style this in the way you want. Reference https://codex.wordpress.org/Conditional_Tags

0
votes

Consider searching for existing plugins prior writing custom functions. For your specific feature, I would use the Page Specific Menu Items plugin.

If you would like to add to an existing function, consider using the body_class filter. With this function, you would be able to add a css class to your body element, then write css that changes how your menu appears for any specific page.