沃克菜单根本不显示

时间:2018-04-25 作者:user8463989

我在尝试使用下拉菜单时遇到问题。这是非wordpress菜单:

<ul class="nav navbar-nav" data-in="fadeInDown" data-out="fadeOutUp">
    <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" >Dropdowns</a>
        <ul class="dropdown-menu">
            <li><a href="#">Custom Menu</a></li>
            <li><a href="#">Custom Menu</a></li>
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown" >Sub Menu</a>
                <ul class="dropdown-menu">
                    <li><a href="#">Custom Menu</a></li>
                    <li><a href="#">Custom Menu</a></li>
                </ul>
            </li>
        </ul>
    </li>
</ul>
我尝试将其转换为wordpress菜单,如下所示:

<div class="collapse navbar-collapse" id="navbar-menu">
<?php 
    wp_nav_menu( array(

        \'menu_class\' => \'nav navbar-nav data-in="fadeInDown" data-out="fadeOutUp"\',
        \'container\' => \'\',
        \'depth\' => 2,
        \'theme_location\' => \'main-menu\',
        \'walker\' => new My_BS_Walker_Nav_Menu(),

    ));
?>
</div>
这是沃克密码,但我只是把自己弄糊涂了:

<?php
if ( ! defined( \'ABSPATH\' ) ) {
    exit; // Exit if accessed directly.
}

/**
 * Custom walker for WordPress custom menus.
 *
 * @link https://wordpress.stackexchange.com/q/298663/137402
 *
 * @see Walker_Nav_Menu
 */
class My_BS_Walker_Nav_Menu extends Walker_Nav_Menu {
    /**
     * Starts the list before the elements are added.
     *
     * @see Walker_Nav_Menu::start_lvl()
     */
    public function start_lvl( &$output, $depth = 0, $args = array() ) {
        // Default class.
        $classes = array( \'dropdown-menu\' );

        /**
         * Filters the CSS class(es) applied to a menu list element.
         */
        $class_names = join( \' \', apply_filters( \'nav_menu_submenu_css_class\', $classes, $args, $depth ) );
        $class_names = $class_names ? \' class="\' . esc_attr( $class_names ) . \'"\' : \'\';

        $output .= \'\' .

                            "<ul{$class_names}>";
    }

    /**
     * Ends the list of after the elements are added.
     *
     * @see Walker_Nav_Menu::end_lvl()
     */
    public function end_lvl( &$output, $depth = 0, $args = array() ) {
        $output .= \'\' .
                            \'</ul>\'; // End .menu-vertical

    }

    /**
     * Starts the element output.
     *
     * @see Walker_Nav_Menu::start_el()
     */
    public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        $item->item_spacing = \'discard\';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        if ( in_array( \'menu-item-has-children\', $classes ) ) {
            $depth2 = intval( $args->depth );
            // Displays a link to the page and not a "dropdown trigger" text.
            if ( $depth2 > 0 && (int) $depth + 1 === $depth2 ) {
                return parent::start_el( $output, $item, $depth, $args, $id );
            }
        } else {
            // Displays a link to the page and not a "dropdown trigger" text.
            return parent::start_el( $output, $item, $depth, $args, $id );
        }

        $classes[] = \'dropdown\';
        $classes[] = \'menu-item-\' . $item->ID;

        /**
         * Filters the CSS class(es) applied to a menu item\'s list item element.
         */
        $class_names = join( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item, $args, $depth ) );
        $class_names = $class_names ? \' class="\' . esc_attr( $class_names ) . \'"\' : \'\';

        $output .= \'<li\' . $class_names .\'>\';

        /** This filter is documented in wp-includes/post-template.php */
        $title = apply_filters( \'the_title\', $item->title, $item->ID );

        /**
         * Filters a menu item\'s title.
         */
        $title = apply_filters( \'nav_menu_item_title\', $title, $item, $args, $depth );

        $item_output = \'<a href="#" class="dropdown-toggle" data-toggle="dropdown" >\';
        $item_output .= $title;
        $item_output .= \'</a>\';

        $output .= $item_output;
    }

    /**
     * Ends the element output, if needed.
     *
     * @see Walker_Nav_Menu::end_el()
     */
    public function end_el( &$output, $item, $depth = 0, $args = array() ) {
        $output .= \'</li>\';
    }

} // My_BS_Walker_Nav_Menu

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

walker代码是可以的,但为了获得此标记:

<ul class="nav navbar-nav" data-in="fadeInDown" data-out="fadeOutUp">
    ...
</ul>
。。在array 传递给wp_nav_menu() 功能,设置items_wrap 收件人:

<ul id="%1$s" class="%2$s" data-in="fadeInDown" data-out="fadeOutUp">%3$s</ul>
因此,完整的标记将是:

<div class="collapse navbar-collapse" id="navbar-menu">
<?php 
    wp_nav_menu( array(

        \'menu_class\' => \'nav navbar-nav\',
        \'container\' => \'\',
        \'depth\' => 2,
        \'theme_location\' => \'main-menu\',
        \'walker\' => new My_BS_Walker_Nav_Menu(),
        \'items_wrap\' => \'<ul id="%1$s" class="%2$s" data-in="fadeInDown" data-out="fadeOutUp">%3$s</ul>\',

    ));
?>
</div>
或可以省略menu_class 参数,只需通过items_wrap 值:

<div class="collapse navbar-collapse" id="navbar-menu">
<?php 
    wp_nav_menu( array(

        \'container\' => \'\',
        \'depth\' => 2,
        \'theme_location\' => \'main-menu\',
        \'walker\' => new My_BS_Walker_Nav_Menu(),
        \'items_wrap\' => \'<ul id="%1$s" class="nav navbar-nav" data-in="fadeInDown" data-out="fadeOutUp">%3$s</ul>\',

    ));
?>
</div>

结束

相关推荐

用标准的干净文件替换NAV-menus.php文件?

我是一个自学成才的wordpress网站设计师,请原谅我的无知。我为一个客户开发的几个网站最近被一个印尼黑客组织破坏了(很有趣!)。客户端没有更新wordpress安装和其他一些危及安全的事情。遗憾的是,没有网站的清理备份。网络主机给了我一个包含恶意软件扫描结果的文本文件。看起来很多受影响的文件都是导航菜单。网站的php文件。文本文件中显示的扫描结果示例如下:“[主目录]/[网站文件夹名称]/wp includes/nav-menu.php:SL-php-INJECTOR-1-ejw.UNOFFICIAL