0

I have this code in my header.php and its working properly except showing li's without menu-item class, its showing them with page-item instead. this is when i dont have a menu created from admin and assigned it to a menu location.

So the menu is wordpress default menu which shows all the pages added. Is there a way to add menu item class with them.

I tried following code but its not working.

function li_nav_class($classes, $item, $args){
    $classes[] = 'menu-item ';
   return $classes;
}
add_filter('nav_menu_css_class' , 'li_nav_class' , 1 , 3);
1

The default fallback in wp_nav_menu for a menu that doesn't exist/isn't registered is wp_page_menu, which unfortunately does not support the same arguments/implement the same level of "customisability" - you either need to create a custom fallback, or remove the fallback entirely (so that the user is forced to set a menu).

| improve this answer | |
0

Try below code with required modifications

<?php
           if (has_nav_menu('primary_navigation')) :
              wp_nav_menu(['theme_location' => 'primary_navigation', 'menu_class' => 'nav']);
           endif;
        ?>

for adding class to ul : 'menu_class' => 'nav'

Also try this link.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.