首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >这种格式可以使用自定义的Nav类吗?

这种格式可以使用自定义的Nav类吗?
EN

WordPress Development用户
提问于 2019-02-21 10:35:09
回答 1查看 112关注 0票数 1

我希望在wordpress菜单上有以下输出

代码语言:javascript
运行
复制
  Link
  Link
  
   Sub Menu Trigger
   
    Sub Menu Link
    Sub Menu Link

然而,我不知道如何使用Nav (我仍在用我的头)。是否有可能:

  • 剥去ul / li
  • 另外,包装子菜单父程序。
  • 包子菜单项的div
  • 通过CMS添加到锚的自定义类

我不确定这是否可能。如果不是的话,还有别的办法可以帮我吗?或者有可能,但很复杂?

EN

回答 1

WordPress Development用户

回答已采纳

发布于 2019-02-21 21:05:52

是的,有可能.你只需要写你自己的沃克课。这里有一个起点:

代码语言:javascript
运行
复制
class My_Custom_Walker_Nav_Menu extends Walker_Nav_Menu {

    public function start_lvl( &$output, $depth = 0, $args = array() ) {
        if ( $depth ) {
            $output .= 'Sub Menu Trigger';
        }
    }

    public function end_lvl( &$output, $depth = 0, $args = array() ) {
        if ( $depth ) {
            $output .= '';
        }
    }

    public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );

        $atts = array();
        $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
        $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
        $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
        $atts['href']   = ! empty( $item->url )        ? $item->url        : '';

        $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );

        $attributes = '';
        foreach ( $atts as $attr => $value ) {
            if ( ! empty( $value ) ) {
                $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
                $attributes .= ' ' . $attr . '="' . $value . '"';
            }
        }

        $title = apply_filters( 'the_title', $item->title, $item->ID );
        $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );

        $item_output = $args->before;
        $item_output .= '';
        $item_output .= $args->link_before . $title . $args->link_after;
        $item_output .= '';
        $item_output .= $args->after;

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

    public function end_el( &$output, $item, $depth = 0, $args = array() ) {
        // don't do anything - elements have no endings
    }

}
票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/329459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档