【问题标题】:Fatal error: Class 'Walker_Nav_Primary' not found致命错误:未找到“Walker_Nav_Primary”类
【发布时间】:2017-09-28 15:35:05
【问题描述】:

我正在尝试更改我网站上导航的标记,而且我对开发 wordpress 还是很陌生,但是我在让它显示我的 Walker 课程时遇到了问题。我正在尝试重新配置 Wordpress 附带的 2017 主题。但是我不断收到这个错误,上面写着“致命错误:在第 56 行的 /wp-content/themes/Vibe/header.php 中找不到类 'Walker_Nav_Primary'” 这是该类的样子:

<?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 .= "\n$indent<ul class=\"dropdown-menu$submenu depth_$depth\">\n";
    }
    /*  
    function start_el( ){ //li a span
    }
    function end_el(){ // closing li a span
    }

    function end_lvl(){ // closing ul

    }
    */
     }

以及我调用它的头文件部分:

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

编辑:

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

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    把他放在你的functions.php文件中

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

    试试这个,如果有问题就回复。

    在你的情况下,应该是

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

    用这段代码更新你的 walker.php 文件

    <?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 .= "\n$indent<ul class=\"dropdown-menu$submenu depth_$depth\">\n";
        }
    }
    

    将function.php文件放在最顶部

    替换

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

    使用此代码。

    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';
    }
    

    【讨论】:

    • 它使站点崩溃:解析错误:语法错误,第 61 行 /home3/wwahidi5102/p2site.wafiwahidi.com/wp-content/themes/Vibe/functions.php 中的意外 '
    • 解析错误:语法错误,文件意外结束,期待 /home3/wwahidi5102/p2site.wafiwahidi.com/wp-content/themes/Vibe/inc/walker.php 中的函数 (T_FUNCTION)第 60 行
    • 我正在使用教程中的示例 walker,我不知道它是否可能是 walker.php
    • 它应该在文件的顶部并确保路径正确。
    • 是的,它是正确的,在顶部,它仍然给我同样的错误。会不会和这个 sn-p 有关://这个主题在两个位置使用 wp_nav_menu()。 register_nav_menus(array('top' => __('Top Menu', 'twentyseventeen'), 'social' => __('Social Links Menu', 'twentyseventeen'), ));这是第 60 行的内容
    【解决方案2】:

    也许需要工作,在头 php 文件中

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

    【讨论】:

    • @leekun 不,它仍然不起作用。我之前尝试过,它似乎忽略了 walker 类。
    • @Rajendran 我应该把它放在哪里? walker.php 文件的目录是/wp-content/themes/Vibe/inc/walker.php。如何让它引用文件?
    【解决方案3】:

    我认为您应该更改此代码!

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

    到这里:

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

    【讨论】:

    • 你在做同样的事情。
    猜你喜欢
    • 2014-08-11
    • 2017-07-27
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多