自定义菜单-第一个菜单项的不同子菜单

时间:2013-07-01 作者:Simon H.

我创建了一个带有下拉菜单的自定义菜单。(只是CSS,没有JavaScript)
我还为第一个菜单项添加了自定义CSS类。

到目前为止还不错。

我遇到的问题是,只为第一个菜单项添加一个自定义下拉列表,这与其他下拉列表不同。此外,我想为第一项设置不同的has children样式。

有人知道怎么做吗?

这是我的定制助行器:

class topmenu_walker_nav_menu extends Walker_Nav_Menu {

    function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
        $id_field = $this->db_fields[\'id\'];
        if ( !empty( $children_elements[$element->$id_field] ) && ( depth == 0 ) ) {
            $element->classes[] = \'has-children\'; // Use any classname you like
        }
        Walker_Nav_Menu::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
    }

function start_lvl( &$output, $depth ) {
    // depth dependent classes
    $indent = ( $depth > 0  ? str_repeat( "\\t", $depth ) : \'\' ); // code indent
    $display_depth = ( $depth + 1); // because it counts the first submenu as 0
    $classes = array(
        \'subnav\',
        ( $display_depth % 2  ? \'menu-odd\' : \'menu-even\' ),
        ( $display_depth >=2 ? \'sub-sub-menu\' : \'\' ),
        \'menu-depth-\' . $display_depth
        );
    $class_names = implode( \' \', $classes );

    // build html
    $output .= "\\n" . $indent . \'<ul class="\' . $class_names . \'">\' . "\\n";
}


 function start_el( &$output, $item, $depth, $args ) {
    global $wp_query;
    $indent = ( $depth > 0 ? str_repeat( "\\t", $depth ) : \'\' ); // code indent

    // depth dependent classes
    $depth_classes = array(
        ( $depth == 0 ? \'main-menu-item\' : \'sub-menu-item\' ),
        ( $depth >=2 ? \'sub-sub-menu-item\' : \'\' ),
        ( $depth % 2 ? \'menu-item-odd\' : \'menu-item-even\' ),
        \'menu-item-depth-\' . $depth
    );
    $depth_class_names = esc_attr( implode( \' \', $depth_classes ) );

    $classes = empty( $item->classes ) ? array() : (array) $item->classes;
    $class_names = esc_attr( implode( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item ) ) );

    $output .= $indent . \'<li id="nav-menu-item-\'. $item->ID . \'" class="\' . $depth_class_names . \' \' . $class_names . \'">\';

    // link attributes
    $attributes  = ! empty( $item->attr_title ) ? \' title="\'  . esc_attr( $item->attr_title ) .\'"\' : \'\';
    $attributes .= ! empty( $item->target )     ? \' target="\' . esc_attr( $item->target     ) .\'"\' : \'\';
    $attributes .= ! empty( $item->xfn )        ? \' rel="\'    . esc_attr( $item->xfn        ) .\'"\' : \'\';
    $attributes .= ! empty( $item->url )        ? \' href="\'   . esc_attr( $item->url        ) .\'"\' : \'\';
    $attributes .= \' class="menu-link \' . ( $depth > 0 ? \'sub-menu-link\' : \'main-menu-link\' ) . \'"\';

    $item_output = sprintf( \'%1$s<a%2$s>%3$s%4$s%5$s</a>%6$s\',
        $args->before,
        $attributes,
        $args->link_before,
        apply_filters( \'the_title\', $item->title, $item->ID ),
        $args->link_after,
        $args->after
    );

    // build html
    $output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
}
}

add_filter( \'wp_nav_menu_objects\', \'tgm_filter_menu_class\', 10, 2 );
/**
 * Filters the first and last nav menu objects in your menus
 * to add custom classes.
 *
 * @since 1.0.0
 *
 * @param object $objects An array of nav menu objects
 * @param object $args Nav menu object args
 * @return object $objects Amended array of nav menu objects with new class
 */
function tgm_filter_menu_class( $objects, $args ) {

    // Only apply the classes to the primary navigation menu
    if ( isset( $args->theme_location ) )
        if ( \'main-menu\' !== $args->theme_location )
            return $objects;

    // Add "first-menu-item" class to the first menu object
    $objects[1]->classes[] = \'settingsitem\';

    // Add "last-menu-item" class to the last menu object
    $objects[count( $objects )]->classes[] = \'last-menu-item\';

    // Return the menu objects
    return $objects;

}


    function output_top_menu() {

    echo \'<div class="nav-container"><div class="container">\';
    wp_nav_menu( array(\'depth\' => 2, \'container_class\' => \'nav\', \'menu_id\' => \'\', \'walker\' => new topmenu_walker_nav_menu(), \'theme_location\'=>\'main-menu\') );  
    echo \'</div></div>\';


    }
这是我的CSS:

.nav-container {
    background: #232323;
        width:100%;
margin-bottom: -5px;
    height: 60px;
    display: inline-block;
}

.nav {
    background: #232323;
    width:996px;
margin-bottom: -5px;
    height: 60px;
    display: inline-block;
}

.nav li {
    float: left;
    list-style-type: none;
    position: relative;
}

.nav li a {
    font-size: 16px; 
    color: white;
    display: block;
    line-height: 60px;
    padding: 0 26px;
    text-decoration: none;
    border-left: 1px solid #2e2e2e;
    font-family: Montserrat, sans-serif;
    text-shadow: 0 0 1px rgba(255, 255, 255, 0.5);
}
.nav li a:hover {
    background-color: #2e2e2e;
}


#settings a {
    padding: 18px;
    height: 24px;
    font-size: 10px;
    line-height: 24px;

}



.settingsitem a {
    font-size: 16px; 
    color: white;
    display: block;
    line-height: 60px;
    padding: 0 26px;
    text-decoration: none;
    border-left: 1px solid #2e2e2e;
    font-family: Montserrat, sans-serif;
    text-shadow: 0 0 1px rgba(255, 255, 255, 0.5);


}
#options a{
    border-left: 0 none;
}
#options>a {
    background-image: url(triangle.png);
    background-position: 85% center;
    background-repeat: no-repeat;
    padding-right: 42px;
}
.subnav {
    visibility: hidden;
    position: absolute;
    top: 110%;
    right: 0;
    width: 200px;
    height: auto;
    opacity: 0;
    transition: all 0.1s;
    background: #232323;
        z-index:1000;
}
.subnav li {
    float: none;
}
.subnav li a {
    border-bottom: 1px solid #2e2e2e;
}
#options:hover .subnav {
    visibility: visible;
    top: 100%;
    opacity: 1;
}

.nav li.has-children a{
    border-left: 1px solid #2e2e2e;
}
.nav li.has-children>a {
    background-image: url(triangle.png);
    background-position: 85% center;
    background-repeat: no-repeat;
    padding-right: 42px;
}

.nav li.has-children:hover .subnav {
    visibility: visible;
    top: 100%;
    opacity: 1;
}

2 个回复
SO网友:jack

好吧,答案取决于你所说的“仅针对第一个菜单项自定义下拉列表,它与其他菜单项下拉列表不同”是什么意思-如何不同?如果只是风格上的,那额外的课程就足够了。您只需确保它的css足够具体,可以覆盖默认值。因此,如果其他下拉列表中的css选择器是

.nav li.has-children a{ ... }
.nav li.has-children>a { ... }
.nav li.has-children:hover .subnav { ... }
然后,为了只覆盖第一项,如果我正确理解了导航标记,您需要这样的内容(在现有css之后):

.nav li.settingsitem a { ... }
.nav li.settingsitem > a { ... }
.nav li.settingsitem:hover .subnav { ... }

SO网友:Musa Haidari

这个问题answered.

简而言之,它表示您需要添加一个子菜单same slug, 和callback function ,以替代第一个默认子菜单。

结束

相关推荐

Wordpress 3.5 dropdown menu

我是WordPress的新手,我想创建自己的下拉菜单,而不使用任何插件。我已经做了一些研究,但有些事情我不明白。在我的头文件中,我的主菜单有以下代码:<div id=\"navigation\"> <?php wp_nav_menu( array( \'theme_location\' => \'primary\' ) ); ?> </div> 我不需要在函数中添加任何代码。php文件,没有它也可以正常工作。在大多数教程中,必须将