【问题标题】:wp_nav_menu() : custom walkerwp_nav_menu() : 自定义walker
【发布时间】:2013-12-09 15:17:54
【问题描述】:

我从 WP 主题开始,我希望使用 wp_nav_menu 功能做类似的菜单已经超过一天了:

      <div class="ui simple dropdown item">
          CATEGORY NAME WITH SUB-CATEGORIES
        <i class="dropdown icon"></i>
          <div class="menu">
            <a href="link_to_sub_category"><div class="item" >Sub Category 1</div></a>
            <a href="link_to_sub_category"><div class="item" >Sub Category 2</div></a>
          </div>
      </div>

另外,如果没有子类别,HTML 输出将是这样的:

      <div class="ui simple dropdown item">
          CATEGORY NAME WITHOUT SUB-CATEGORY
      </div>

我必须在我的 functions.php 中添加什么? 任何提示或任何帮助将不胜感激!

谢谢。

【问题讨论】:

    标签: wordpress menu theming


    【解决方案1】:

    在functions.php中添加下一个代码:

    class SH_Nav_Menu_Walker extends Walker {
        var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
        var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
    
        function start_lvl(&$output, $depth) {
            $indent = str_repeat("\t", $depth);
            $output .= "\n$indent";
            $output .= "<i class=\"dropdown icon\"></i>\n";
            $output .= "<div class=\"menu\">\n";
        }
    
        function end_lvl(&$output, $depth) {
            $indent = str_repeat("\t", $depth);
            $output .= "$indent</div>\n";
        }
    
        function start_el(&$output, $item, $depth, $args) {
            $value = '';
            $classes = empty( $item->classes ) ? array() : (array) $item->classes;
            $classes = in_array( 'current-menu-item', $classes ) ? array( 'current-menu-item' ) : array();
            $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
            $class_names = strlen( trim( $class_names ) ) > 0 ? ' class="' . esc_attr( $class_names ) . '"' : '';
            $id = apply_filters( 'nav_menu_item_id', '', $item, $args );
            $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
            $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        ) .'"' : '';
            $item_output = $args->before;
            $item_output .= '<a'. $attributes . $id . $value . $class_names . '>';
            $item_output .= '<div class="item">';
            $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
            $item_output .= '</div>';
            $item_output .= "</a>\n";
            $item_output .= $args->after;
            $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
        }
    }
    

    并使用下一个参数在“正确”位置调用 wp_nav_menu:

    wp_nav_menu(array('items_wrap' => '<div id="%1$s" class="%2$s ui simple dropdown item">%3$s</div>', 'theme_location' => 'sidebar_right_menu', 'walker' => new SH_Nav_Menu_Walker))
    

    【讨论】:

    • 这不是我真正想要的,但你帮了我很多。我还已经更改了您的代码中的一些内容以满足我的需要,但现在我在子元素上遇到了另一个问题。你介意帮忙吗?这是我的代码:snipt.org/Byx0 和 snipt.org/Byy8
    • 孩子总是在他们的班级上添加“ui simple dropdown”,而我只想为孩子添加“item”。
    • 也许我没有理解你的问题......只有子元素应该有类“项目”? *在您的代码中。如果不是 $args->has_children,那么 $item_output .= "
      \n";
    • 是的,没错,只有子元素应该有类“item”。喜欢这个代码:snipt.org/Byz5
    • Try: if (!$args->has_children //not parent && $depth != 0 //使用深度排除根元素) $item_output .= "
      \n";
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签