WordPress菜单项>子菜单类更新

时间:2018-09-08 作者:tassawarr

我是个新手WordPress coredevelopment process. 因为我对WordPress functions. But I need the help from you guys.

实际上,我正在开发我的第一个主题,我已经有了CSS for my menu. 菜单已在中注册functions.php.

我用过code posted below 添加我的类"mainnav-section".

<?php 
              $args = [
                \'theme_location\' => \'header-menu\',
                \'container\'      => \'ul\',
                \'container_class\'=> \'\',
                \'menu_class\'     => \'mainnav-section\'
              ];
              wp_nav_menu($args);
          ?>

And it executed this.

<ul id="menu-header" class="mainnav-section">
    <li id="menu-item-511" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-511">
        <a href="http://#">Latest</a>
        <ul class="sub-menu">
            <li id="menu-item-512" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-512">
                <a href="http://#">HardFork</a>
            </li>
        </ul>
    </li>
    <li id="menu-item-513" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-513">
        <a href="http://#">Innovations</a>
    </li>
</ul>
我只想添加CSS classmain-menu items, sub-menulist items. 因为我有CSS and JS completed 获取desired menu.

1 个回复
SO网友:Howdy_McGee

下面将添加一个名为class-here 至所有菜单项:

/**
 * Filters the CSS class(es) applied to a menu item\'s list item element.
 *
 * @param array    $classes The CSS classes that are applied to the menu item\'s `<li>` element.
 * @param WP_Post  $item    The current menu item.
 * @param stdClass $args    An object of wp_nav_menu() arguments.
 * @param int      $depth   Depth of menu item. Used for padding.
 *
 * @return Array $classes
 */
function theme_prefix_nav_menu_css_class( $classes, $item, $args, $dpeth ) {

    $classes[] = \'class-here\';

    return $classes;

}
add_filter( \'nav_menu_css_class\', \'theme_prefix_nav_menu_css_class\', 10, 4 );
您可以将此添加到主题中functions.php 文件WordPress WP Nav Walker创建菜单时还有一系列其他挂钩-Check out The Code Reference

结束

相关推荐

Display Menu Navigation Label

如何获得菜单中应有的导航标签我正在使用这个代码 <?php $locations = get_registered_nav_menus(); $menus = wp_get_nav_menus(); $menu_locations = get_nav_menu_locations(); $location_id = \'footer_menu1\'; if (isset($menu_loc