Any walker causes blank menu?

时间:2014-02-08 作者:Ryan Leach

使用以下或任何其他自定义Walker\\u Nav\\u菜单似乎会导致导航菜单为空,我尝试指定默认值以确保我没有发疯,但它仍然会将输出为空。。。有什么想法吗?

<?php wp_nav_menu( array( \'theme_location\' => \'primary\', \'depth\' => 3, \'walker\' => new Walker_Nav_Menu() ) ); ?>
在上下文中

<?php
/**
 * The Header for our theme.
 *
 * Displays all of the <head> section and everything up till <div id="main">
 *
 * @package Expound
 */
?><!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( \'charset\' ); ?>" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title><?php wp_title( \'|\', true, \'right\' ); ?></title>
<link rel="profile" href="http://gmpg.org/xfn/11" />
<link rel="pingback" href="<?php bloginfo( \'pingback_url\' ); ?>" />
<!--[if lt IE 9]>
<script src="<?php echo get_template_directory_uri(); ?>/js/html5.js" type="text/javascript"></script>
<![endif]-->

<?php wp_head(); ?>
</head>

<body <?php body_class(); ?>>
<div id="page" class="hfeed site">
    <?php do_action( \'expound_header_before\' ); ?>
    <header id="masthead" class="site-header" role="banner">
        <div class="site-branding">
            <div class="site-title-group">
                <a href="<?php echo esc_url( home_url( \'/\' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( \'name\', \'display\' ) ); ?>" rel="home">
                    <!--div class="site-title"><!--?php bloginfo( \'name\' ); ?></div-->
                    <img height="240" width="722" alt="The All Australian Minecraft Community" class="wp-post-image" src="http://taamc.com/wordpress/wp-content/themes/expound-child/images/logo.png">
                    <!--h2 class="site-description"--><!--?php bloginfo( \'description\' ); ?--><!--/h2-->
                    <!--div id="sub-title">
                        <div id="sub-title-image"></div>
                    </div-->
                </a>
            </div>
        </div>
        <div class = "site-title-group">
                <?php if ( dynamic_sidebar(\'header_widget_area\') ) : else : endif; ?>
        </div>
        <nav id="site-navigation" class="navigation-main" role="navigation">
            <h1 class="menu-toggle"><?php _e( \'Menu\', \'expound\' ); ?></h1>
            <div class="screen-reader-text skip-link"><a href="#content" title="<?php esc_attr_e( \'Skip to content\', \'expound\' ); ?>"><?php _e( \'Skip to content\', \'expound\' ); ?></a></div>
####################################################################################    
<?php wp_nav_menu( array( \'theme_location\' => \'primary\', \'depth\' => 3, \'walker\' => new Walker_Nav_Menu()) ); ?>
####################################################################################
            <div class="site-nav-search"><?php if ( dynamic_sidebar(\'navigation_widget_area\') ) : else : endif; ?></div>
            <?php do_action( \'expound_navigation_after\' ); ?>
        </nav><!-- #site-navigation -->
    </header><!-- #masthead -->
    <?php do_action( \'expound_header_after\' ); ?>

    <div id="main" class="site-main">
以上内容当前位于我的标题中。php的子主题,如果这有什么不同,原始主题也使用默认的walker,但没有明确说明。

使用如上所述的php

        <div class="menu">
            <ul class=" nav-menu">
            <li class="current_page_item">
                <a href="http://taamc.com/">Home</a>
            </li>
            <li id="menu-item-21" class="menu-item-21">
                <a></a>
            </li>
            <li id="menu-item-87" class="menu-item-87">
                <a></a>
            </li>
            <li id="menu-item-122" class="menu-item-122">
                <a></a>
            </li>
            <li id="menu-item-28" class="menu-item-28">
                <a></a>
            </li>
            <li id="menu-item-127" class="menu-item-127">
                <a></a>
            </li>
            <li id="menu-item-22" class="menu-item-22">
                <a></a>
            </li>
        </ul>
    </div>
给出错误:

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 148

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 151

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 151

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 153

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 148

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 151

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 151

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 153

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 148

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 151

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 151

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 153

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 148

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 151

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 151

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 153

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 148

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 151

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 151

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 153

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 148

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 151

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 151

Notice: Trying to get property of non-object in /home/taamc/public_html/wordpress/wp-includes/nav-menu-template.php on line 153
使用主题中默认的php

<?php wp_nav_menu( array( \'theme_location\' => \'primary\', \'depth\' => 3) ); ?>

<div class="menu">
    <ul class=" nav-menu">
    <li class="current_page_item">
        <a href="http://taamc.com/">Home</a>
    </li>
    <li class="page_item page-item-21">
        <a href="http://taamc.com/activity/">Activity</a>
    </li>
    <li class="page_item page-item-87">
        <a href="http://taamc.com/gallery/">Gallery</a>
    </li>
    <li class="page_item page-item-122">
        <a href="http://taamc.com/getting-started/">Getting Started</a>
    </li>
    <li class="page_item page-item-28">
        <a href="http://taamc.com/groups/">Groups</a>
    </li>
    <li class="page_item page-item-127">
        <a href="http://taamc.com/map/">Map</a>
    </li>
    <li class="page_item page-item-22">
        <a href="http://taamc.com/members/">Members</a>
    </li>
    </ul>
</div>

6 个回复
SO网友:Taig

您需要确保菜单已正确注册到functions.php 然后在WP仪表板中激活。

add_action( \'init\', register_nav_menu( \'navigation\', __( \'Navigation\' ) ) );
否则,我只会面临一长串空头支票。

http://codex.wordpress.org/Navigation_Menus

SO网友:adrian7

您应该将实际对象作为“walker”传递,而不是动态创建。正确的方法是:

$myWalker = new Walker_Nav_Menu();
wp_nav_menu( array( \'theme_location\' => \'primary\', \'depth\' => 3, \'walker\' => $myWalker ) ); ?>
还要确保在自定义类中扩展Walker\\u Nav\\u菜单。

SO网友:Dhanuka Nuwan

您是否使用walker功能表进行移动响应?如果是,那么您可以寻求另一种解决方案。(仅当您无法使用walker菜单修复错误时)

您可以在移动设备上隐藏主菜单,也可以使用另一个主菜单。下面是一个示例

<div class="m-sec-nav">
        <?php 
        $menu_name = \'careers-menu\'; //your menu theme_location
        if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
                $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
                $menu_items = wp_get_nav_menu_items($menu->term_id); ?>
                    <select onchange="location = this.options[this.selectedIndex].value;">
                    <?php foreach ( (array) $menu_items as $key => $menu_item ) { ?>
                        <option value="<?php echo $menu_item->url; ?>"><?php echo $menu_item->title; ?></option>
                    <?php } ?>
                    </select>
                <?php } ?>

    </div>

SO网友:Firestorm_dev

我认为整个问题是您试图使用默认的walker,但通过在walker字段中声明它,告诉wordpress您试图加载自定义walker,如果保留为空,则默认情况下会加载wp walker。

两个选项:

1/更改\'walker\' => new Walker_Nav_Menu() to \'walker\' => \'\' 默认情况下称为。

2/将声明编辑为新名称,如:new themeslug\\u Walker\\u Nav\\u Menu(),然后将以下内容添加到函数中。php:


class themeslug_walker_nav_menu extends Walker_Nav_Menu {

// add classes to ul sub-menus
    function start_lvl( &$output, $depth ) {
        // depth dependent classes
        $indent = ( $depth > 0  ? str_repeat( "\\t", $depth ) : \'\' ); // code indent
        $display_depth = ( $depth + 1); // because it counts the first submenu as 0
        $classes = array(
            \'sub-menu\',
            ( $display_depth % 2  ? \'menu-odd\' : \'menu-even\' ),
            ( $display_depth >=2 ? \'sub-sub-menu\' : \'\' ),
            \'menu-depth-\' . $display_depth
            );
        $class_names = implode( \' \', $classes );

        // build html
        $output .= "\\n" . $indent . \'\' . "\\n";
    }

    // add main/sub classes to li\'s and links
     function start_el( &$output, $item, $depth, $args ) {
        global $wp_query;
        $indent = ( $depth > 0 ? str_repeat( "\\t", $depth ) : \'\' ); // code indent

        // depth dependent classes
        $depth_classes = array(
            ( $depth == 0 ? \'main-menu-item\' : \'sub-menu-item\' ),
            ( $depth >=2 ? \'sub-sub-menu-item\' : \'\' ),
            ( $depth % 2 ? \'menu-item-odd\' : \'menu-item-even\' ),
            \'menu-item-depth-\' . $depth
        );
        $depth_class_names = esc_attr( implode( \' \', $depth_classes ) );

        // passed classes
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $class_names = esc_attr( implode( \' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item ) ) );

        // build html
        $output .= $indent . \'ID . \'" class="\' . $depth_class_names . \' \' . $class_names . \'">\';

        // link attributes
        $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        ) .\'"\' : \'\';
        $attributes .= \' class="menu-link \' . ( $depth > 0 ? \'sub-menu-link\' : \'main-menu-link\' ) . \'"\';

        $item_output = sprintf( \'%1$s%3$s%4$s%5$s%6$s\',
            $args->before,
            $attributes,
            $args->link_before,
            apply_filters( \'the_title\', $item->title, $item->ID ),
            $args->link_after,
            $args->after
        );

        // build html
        $output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
    }
}
参见法典,其解释相当清楚。

SO网友:Clint Stegman

有时,在尚未创建菜单时会发生这种情况。无法使用或扩展Walker_Nav_Menu 类是因为调用primary 没有助行器的导航菜单实际上是wp_page_menu(). 要使用助行器,需要创建菜单并选中主复选框。

SO网友:Nirav Patel

自定义\\u Nav\\u Walker putfunction.php 文件

add_theme_support( \'nav-menus\' );

class Custom_Nav_Walker extends Walker_Nav_Menu {

    function start_lvl(&$output, $depth = 0, $args = array()) {
        $output .= "\\n<div class=\\"submenu\\">\\n";
    }

    function end_lvl(&$output, $depth = 0, $args = array()) {
        $output .= "\\n</div>\\n";
    }

    function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
        parent::start_el( $item_html, $item, $depth, $args );
        if ( $item->is_dropdown && ( $depth === 0 ) ) {
            $output .= "<a href=\\"".esc_url($item->url)."\\">".esc_attr($item->title)."</a>";
        } elseif ($depth === 0) {
            $output .= "<a href=\\"".esc_url($item->url)."\\">".esc_attr($item->title)."</a>";
        } elseif ($depth > 0) {
            $output .= "<a href=\\"".esc_url($item->url)."\\">".esc_attr($item->title)."</a>";
        }
    }

nav\\U菜单输入header.php

<?php
$defaults = array(
    \'theme_location\'  => \'\',
    \'menu\'            => \'Project Nav\', //menu name
    \'container\'       => false,
    \'container_class\' => \' \',
    \'container_id\'    => \'\',
    \'menu_class\'      => \'nav\',
    \'menu_id\'         => \'\',
    \'echo\'            => true,
    \'before\'          => \'\',
    \'after\'           => \'\',
    \'link_before\'     => \'\',
    \'link_after\'      => \'\',
    \'items_wrap\'      => \'<div id="%1$s" class="%2$s">%3$s</div>\',
    \'depth\'           => 0,
    \'walker\'          => new Custom_Nav_Walker() 
);

wp_nav_menu( $defaults );
?>

结束

相关推荐

Use menus with anchors

我有一个CPT归档页面,我想创建一个导航菜单,该菜单将跳转到表单中的锚定<a name=\"post-slug\" http=\"{the permalink goes here}>The CPT Title</a>我已经能够在模板文件中创建锚定,所以这不是问题对于页面顶部的链接本身,我可以手动创建它们,也可以使用WordPress菜单系统中的自定义链接。。。。但是有很多,所以我宁愿尝试在WordPress菜单系统中创建一个常规菜单。问题是,我需要以某种方式调整它的输出,以更改常规