wp_nav_menu() customized

时间:2011-08-11 作者:dani

<div class="example">
<h1>Menu</h1>
<ul>
    <li>sub-menu</li>
    <li>sub-menu</li>
    <li>sub-menu</li>
</ul>
</div><!-- end of example -->

<div class="example>
<h1>Menu</h1>
<ul>
    <li>sub-menu</li>
    <li>sub-menu</li>
    <li>sub-menu</li>
</ul>
</div><!-- end of example -->
我正试图用wp\\u nav\\u menu()创建一个菜单,我想实现这样一个干净的代码。我想用一个定制的助行器来做,但我没有这方面的经验。我想我必须重写一个类,但是。。。

你能帮我查一下密码吗??谢谢大家

1 个回复
最合适的回答,由SO网友:fuxia 整理而成

在我的基本主题中,我使用助行器来简化输出:

<?php # -*- coding: utf-8 -*-
/**
 * Create a nav menu with very basic markup.
 *
 */
class T5_Nav_Menu_Walker_Simple 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 $args    Additional strings.
     * @return void
     */
    public function start_el( &$output, $item, $depth, $args )
    {
        $output     .= \'<li>\';
        $attributes  = \'\';

        ! empty ( $item->attr_title )
            // Avoid redundant titles
            and $item->attr_title !== $item->title
            and $attributes .= \' title="\' . esc_attr( $item->attr_title ) .\'"\';

        ! empty ( $item->url )
            and $attributes .= \' href="\' . esc_attr( $item->url ) .\'"\';

        $attributes  = trim( $attributes );
        $title       = apply_filters( \'the_title\', $item->title, $item->ID );
        $item_output = "$args->before<a $attributes>$args->link_before$title</a>"
                        . "$args->link_after$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
        );
    }

    /**
     * @see Walker::start_lvl()
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @return void
     */
    public function start_lvl( &$output )
    {
        $output .= \'<ul class="sub-menu">\';
    }

    /**
     * @see Walker::end_lvl()
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @return void
     */
    public function end_lvl( &$output )
    {
        $output .= \'</ul>\';
    }

    /**
     * @see Walker::end_el()
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @return void
     */
    function end_el( &$output )
    {
        $output .= \'</li>\';
    }
}
请参见this answer 有关实施详细信息。

结束

相关推荐

Menu API not switching menus?

我正在使用菜单API,我想切换到其他菜单,但出于某种原因,它保留了第一个菜单这是我的密码在函数中。php add_action( \'init\', \'register_my_menus\',10 ); function register_my_menus() { register_nav_menu(\'main-navigation\', \'Main Navigation\'); } 下面是我的主题文件(header.ph