wp_nav_menu()依赖的<a>和<li>标记中是否包含要包含类的函数?

时间:2019-11-01 09:29:53

标签: wordpress

  'header_menu',                             'menu_class'=>'navbar-nav',                             'container'=>'div',                             'container_class'=>'导航栏合拢折叠对齐内容中心',                             'container_id'=>'navbarDefault',                             '深度'=> 2,                         )                 );       ?>

3 个答案:

答案 0 :(得分:0)

否,您将需要在此处创建自己的菜单类;

https://code.tutsplus.com/tutorials/understanding-the-walker-class--wp-25401 https://wordpress.stackexchange.com/questions/248369/how-to-create-this-custom-menu-walker

https://developer.wordpress.org/reference/classes/walker/

通过这种方式,您可以根据需要编辑和创建菜单。

注意:您可以通过wordpress面板中的菜单添加课程,但是您需要手动添加所有课程。

答案 1 :(得分:0)

您可以通过“ nav_menu_css_class”过滤器来做到这一点:

function wpdocs_special_nav_class( $classes, $item ) {
        // Apply specific class: .special-class
    $classes[] = "special-class";
    return $classes;
}
add_filter( 'nav_menu_css_class' , 'wpdocs_special_nav_class' , 10, 2 );

参考文献:

Add custom CSS classes to wp_nav_menu’s HTML output using WordPress filters

Codex

答案 2 :(得分:0)

/**
 * Add class to the li element on a menu.
 *
 * @param $classes
 * @param $item
 * @param $args
 *
 * @return array
 */
function add_classes_on_li( $classes, $item, $args ) {
    if( 'primary' === $args->theme_location ) {
        $classes[] = 'nav-item';
    }

    if( 'secondary' === $args->theme_location ) {
        $classes[] = 'nav-item';
    }

    return $classes;
}
add_filter( 'nav_menu_css_class', 'add_classes_on_li', 1, 3 );

/**
 * Add class to primary menu anchors.
 *
 * @param $atts
 * @param $item
 * @param $args
 * @param $depth
 *
 * @return array
 */
function add_class_to_primary_menu_anchors( $atts, $item, $args, $depth ) {
    if( 'primary' === $args->theme_location ) {
        $atts['class'] = 'nav-link';
    }

    return $atts;
}
add_filter( 'nav_menu_link_attributes', 'add_class_to_primary_menu_anchors', 10, 4 );