致命错误:未找到“Walker_Nav_Primary”类

致命错误:未找到“Walker_Nav_Primary”类

问题描述:

I am trying to change the markup for the nav on my site and I am pretty new to developing wordpress, but I am having issues getting it to display my Walker class. I am attempting to reconfigure the 2017 theme that comes with Wordpress. But I keep getting this error that says "Fatal error: Class 'Walker_Nav_Primary' not found in /wp-content/themes/Vibe/header.php on line 56" This is what the class looks like:

<?php
  class Walker_Nav_Primary extends Walker_Nav_menu {

    function start_lvl( &$output, $depth = 0, $args = array() ){ //ul
        $indent = str_repeat("\t",$depth);
        $submenu = ($depth > 0) ? ' sub-menu' : '';
        $output .= "
$indent<ul class=\"dropdown-menu$submenu depth_$depth\">
";
    }
    /*  
    function start_el( ){ //li a span
    }
    function end_el(){ // closing li a span
    }

    function end_lvl(){ // closing ul

    }
    */
     }

And the portion of my header file where I am calling it:

<?php wp_nav_menu( array(
    'theme_location' => 'top',
    'menu_id'        => 'primary-menu',
    'menu_class'        => 'head-menu',
    'walker'            =>  new Walker_Nav_Primary(),
) ); ?>

EDIT:

// This theme uses wp_nav_menu() in two locations.
register_nav_menus( array(
    'top'    => __( 'Top Menu', 'twentyseventeen' ),
    'social' => __( 'Social Links Menu', 'twentyseventeen' ),
) );

我正在尝试更改我网站上导航的标记,而且我是开发wordpress的新手,但是我 我有问题让它显示我的沃克课程。 我正在尝试重新配置Wordpress附带的2017主题。 但是我一直收到这样的错误,上面写着“致命的错误:Class'Walker_Nav_Primary'在第56行的/wp-content/themes/Vibe/header.php中找不到”这就是这个类的样子: p> \ n

 &lt;?php 
 class Walker_Nav_Primary扩展了Walker_Nav_menu {
 
函数start_lvl(&amp; $ output,$ depth = 0,$ args = array()){// ul 
 $ indent  = str_repeat(“\ t”,$ depth); 
 $ submenu =($ depth&gt; 0)?  '子菜单':''; 
 $ output。=“
 $ indent&lt; ul class = \”dropdown-menu $ submenu depth_ $ depth \“&gt; 
”; 
} 
 / * \  n function start_el(){// li a span 
} 
 function end_el(){// close li a span 
} 
 
函数end_lvl(){//关闭ul 
 
} 
  * / 
} 
  code>  pre> 
 
 

我的头文件中我称之为的部分: p>

 &lt  ;?php wp_nav_menu(array(
'theme_location'=&gt;'top',
'menu_id'=&gt;'primary-menu',
'menu_class'=&gt;'head-menu',
'  walker'=&gt; new Walker_Nav_Primary(),
));  ?&gt; 
  code>  pre> 
 
 

编辑: p>

  //此主题在两个位置使用wp_nav_menu()。
register_nav_menus  (数组(
'top'=&gt; __('Top Menu','twentyseventeen'),
'ocialical'=&gt; __('Social Links Menu','twentyseventeen'),
)); \  n  code>  pre> 
  div>

Put his in your functions.php file

// Register Custom Navigation Walker
require_once get_template_directory() . '/walker-fie-name.php';

Try this and reply if any issue.

In your case, it should be

require_once get_template_directory() . '/inc/walker.php';

Update you walker.php file with this code

<?php
if ( ! class_exists( 'Walker_Nav_Primary' ) ) {
  class Walker_Nav_Primary extends Walker_Nav_menu {

    public function start_lvl( &$output, $depth = 0, $args = array() ){ //ul
        $indent = str_repeat("\t",$depth);
        $submenu = ($depth > 0) ? ' sub-menu' : '';
        $output .= "
$indent<ul class=\"dropdown-menu$submenu depth_$depth\">
";
    }
}

Put in function.php file at very top

Replace

require_once get_template_directory() . '/inc/walker.php';

With this code.

if ( ! file_exists( get_template_directory() . '/inc/walker.php' ) ) {
    // file does not exist... return an empty page with msg not found
    wp_die('Not found');
} else {
    // file exists... require it.
    require_once get_template_directory() . '/inc/walker.php';
}

Maybe require works , in header php file

require_once('xxx/xxx/Walker_Nav_Primary.php');

I think you should change this code!

<?php wp_nav_menu( array(
'theme_location' => 'top',
'menu_id'        => 'primary-menu',
'menu_class'        => 'head-menu',
'walker'            =>  new Walker_Nav_Primary(),) ); ?>

to this:

    <?php 
$walker = new Walker_Nav_Primary;
wp_nav_menu( array(
    'theme_location' => 'top',
    'menu_id'        => 'primary-menu',
    'menu_class'        => 'head-menu',
    'walker'            =>  $walker,) ); ?>