首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WP菜单-自定义wordpress菜单HTML结构

WP菜单-自定义wordpress菜单HTML结构
EN

Stack Overflow用户
提问于 2017-09-20 08:12:38
回答 1查看 1.3K关注 0票数 0

如何使用自定义HTML结构创建Wordpress菜单

代码语言:javascript
复制
<?php wp_nav_menu( array( 'theme_location' => 'global-menu' ) ); ?>

输出HTML应该如下所示:

代码语言:javascript
复制
<nav>
   <a href="" class="item-link item-dynamic-class-1">Item 1</a>
   <a href="" class="item-link item-dynamic-class-2">Item 2</a>
   <a href="" class="item-link item-dynamic-class-3">Item 3</a>
   <a href="" class="item-link item-dynamic-class-4">Item 4</a>
</nav>

我希望item-dynamic-class-x是动态设置从Wordpress菜单编辑器(CSS类输入字段)。如果已填充,则将类添加到锚点。如果没有,那么应该只出现静态的条目链接类。

EN

回答 1

Stack Overflow用户

发布于 2017-09-20 08:49:48

尝试使用下面的代码

代码语言:javascript
复制
wp_nav_menu(
 array (
    'menu'            => 'main-menu',
    'container'       => '',// or false
    'container_id'    => FALSE,
    'menu_class'      => '',
    'menu_id'         => FALSE,
    'items_wrap'    => '%3$s',
    'depth'           => 1,
    'walker'          => new Description_Walker
 )
);

将下面的代码放在活动的functions.php中

代码语言:javascript
复制
class Description_Walker extends Walker_Nav_Menu
{
    /**
     * Start the element output.
     *
     * @param  string $output Passed by reference. Used to append additional content.
     * @param  object $item   Menu item data object.
     * @param  int $depth     Depth of menu item. May be used for padding.
     * @param  array|object $args    Additional strings. Actually always an 
     * @return void
     */
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 )
    {
        $classes     = empty ( $item->classes ) ? array () : (array) $item->classes;

        $class_names = join(
            ' '
        ,   apply_filters(
                'nav_menu_css_class'
            ,   array_filter( $classes ), $item
            )
        );

        ! empty ( $class_names )
            and $class_names = ' class="'. esc_attr( $class_names ) . '"';



        $attributes  = '';

        ! empty( $item->attr_title )
            and $attributes .= ' title="'  . esc_attr( $item->attr_title ) .'"';
        ! empty( $item->target )
            and $attributes .= ' target="' . esc_attr( $item->target     ) .'"';
        ! empty( $item->xfn )
            and $attributes .= ' rel="'    . esc_attr( $item->xfn        ) .'"';
        ! empty( $item->url )
            and $attributes .= ' href="'   . esc_attr( $item->url        ) .'"';

        // insert description for top level elements only
        // you may change this
        $description = ( ! empty ( $item->description ) and 0 == $depth )
            ? '<small class="nav_desc">' . esc_attr( $item->description ) . '</small>' : '';

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

        $item_output = $args->before
            . "<a $attributes>"
            . $args->link_before
            . $title
            . '</a> '
            . $args->link_after
            . $description
            . $args->after;

        // Since $output is called by reference we don't need to return anything.
        $output .= apply_filters(
            'walker_nav_menu_start_el'
        ,   $item_output
        ,   $item
        ,   $depth
        ,   $args
        );
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46316693

复制
相关文章

相似问题

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