wordpress 如何将下拉列表类添加到wp_nav_menu()函数中

7kqas0il  于 2022-11-02  发布在  WordPress
关注(0)|答案(2)|浏览(101)

如何在wp_nav_menu函数中通过 li -<li class="dropdown"> Package 的类中添加dropdown类?
我的静态菜单

<ul>
<li>
<a href="#hero">Home</a></li>
      <li><a href="blog.html">Blog</a></li>
      <li class="dropdown"><a href="#"><span>Drop Down</span> <i class="bi bi-chevron-down dropdown-indicator"></i></a>
        <ul>
          <li><a href="#">Drop Down 1</a></li>
          <li class="dropdown"><a href="#"><span>Deep Drop Down</span> <i class="bi bi-chevron-down dropdown-indicator"></i></a>
            <ul>
              <li><a href="#">Deep Drop Down 1</a></li>
            </ul>
          </li>
          <li><a href="#">Drop Down 2</a></li>
        </ul>
      </li>
</ul>

我有这个动态导航菜单功能,

<?php
if ( has_nav_menu( 'main-menu' ) ) :
    wp_nav_menu( array(
        'theme_location' => 'main-menu',
        'items_wrap'     => '%3$s',
        'add_li_class'   => '',
        'container' => ''
    ));
endif;
?>

如何在wordpress的wp_nav_menu函数中使用下拉菜单使静态菜单动态化?

polhcujo

polhcujo1#

你可以用这个

if ( has_nav_menu( 'main-menu' ) ) :
    wp_nav_menu( array(
        'theme_location' => 'main-menu',
        'items_wrap'     => '%3$s',
        'menu_class'   => 'menu_class_name', // (string) CSS class to use for the ul element which forms the menu. Default 'menu'. and you can use .menu_class_name li {} for css
        'container' => '' 
    ));
endif;
6jjcrrmo

6jjcrrmo2#

你有两个选择。
您可以在wordpress后端添加自定义类。https://sevenspark.com/how-to/how-to-add-a-custom-class-to-a-wordpress-menu-item
或者您可以在functions.php中使用这个自定义的php代码。

function add_menu_item_class( $classes, $item ) {       
    $classes[] = YOURCLASSNAME;
    return $classes;
} 

add_filter( 'nav_menu_css_class' , 'add_menu_item_class' , 10, 2 );

相关问题