Adding class to li elements in wp_nav_menu

前端 未结 2 1797
情话喂你
情话喂你 2021-01-03 08:21

I have problem with wp_nav_menu in wordpress. I want to make structure of Li elements, where all of them will have class \"menu-li\". But It doesn\'t work for me. I have thi

2条回答
  •  死守一世寂寞
    2021-01-03 08:44

    The menu_class parameter of wp_nav_menu() controls the class added to the

      wrapper element, rather than the individual
    • 's. To add classes to the menu items, you want to use the nav_menu_css_class filter.

      Here's how it works (add this to your theme's functions.php file):

      add_filter ( 'nav_menu_css_class', 'so_37823371_menu_item_class', 10, 4 );
      
      function so_37823371_menu_item_class ( $classes, $item, $args, $depth ){
        $classes[] = 'menu-li';
        return $classes;
      }
      

      This will add the menu-li class to every menu item.

      You can also do this conditionally based on the $item, $args etc. passed to this filter if you like.

提交回复
热议问题