为什么wordpress wp_nav_menu在search.php上为空?

时间:2018-01-09 14:33:01

标签: php wordpress wp-nav-walker

出于某种原因,我在Wordpress中的导航步行器正在为菜单吐出空的项目,但仅在search.php页面上。这是输出的示例:

ffmpeg.exe -i "movie.mp4" -itsoffset 3.84 -i "movie.mp4" -map 0:v -map 1:a -vcodec copy -acodec copy "movie-audio-delayed.mp4"

以下是生成html的代码:

    <nav class="main-nav" role="navigation">
         <div class="menu">
           <ul>
             <li id="menu-item-1671"><a target="_blank"><span>→</span></a></li>
             <li id="menu-item-3022"><a target="_blank"><span>→</span></a></li>
             <li id="menu-item-3024"><a target="_blank"><span>→</span></a></li>
             <li id="menu-item-2816"><a target="_blank"><span>→</span></a></li>

             <!-- etc... -->

          </ul>
     </div>
   </nav>

这是functions.php中的导航助手代码:

<nav class="main-nav" role="navigation">
      <?php  
           wp_nav_menu(array(  
                  'menu' => 'Main Menu', 
                  'container_id' => 'cssmenu', 
                  'walker' => new CSS_Menu_Maker_Walker()
            )); 
      ?>
</nav>

因此,除了search.php之外,这在每个页面上都能正常工作。我唯一能想到的是,也许来自header.php的这个代码(当然是由search.php调用的)与命名空间有些冲突:

class CSS_Menu_Maker_Walker extends Walker {

  var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );

  function start_lvl( &$output, $depth = 0, $args = array() ) {
    $indent = str_repeat("\t", $depth);
    $output .= "\n$indent<ul>\n";
  }

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

  function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {

    global $wp_query;
    $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    $class_names = $value = '';        
    $classes = empty( $item->classes ) ? array() : (array) $item->classes;

    /* Add active class */
    if(in_array('current-menu-item', $classes)) {
      $classes[] = 'active';
      unset($classes['current-menu-item']);
    }

    /* Check for children */
    $children = get_posts(array('post_type' => 'nav_menu_item', 'nopaging' => true, 'numberposts' => 1, 'meta_key' => '_menu_item_menu_item_parent', 'meta_value' => $item->ID));
    if (!empty($children)) {
      $classes[] = 'has-sub';
    }

    $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
    $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

    $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
    $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

    $output .= $indent . '<li' . $id . $value . $class_names .'>';

    $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        ) .'"' : '';

    $item_output = $args->before;
    $item_output .= '<a'. $attributes .'><span>';
    $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    $item_output .= '</span></a>';
    $item_output .= $args->after;

    $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  }

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

以前有过这种事吗?你能看到我失踪的东西吗?

编辑在is_search()

之后添加了代码

所以我终于在开发服务器上进行了调试(搜索不能在我的本地工作),看起来这两行导致错误(靠近导航步行器代码的末尾):

 <?php
        if(isset($_GET['filter']) && $_GET['filter'] != ''):
                $filters = explode(',',$_GET['filter']);
        else:
                $filters = array();
        endif;
        if(is_search()):
    ?>

<div class="advanced-search <?php if(!empty($filters)) echo 'open'; ?>">

        <div class="primary-filter row">
            <div class="wrapper">
                <span class="label">FILTER BY:</span>

                <ul class="filter-list primary">
                    <li data-target="application"><a id="application-link" href="#">Research Area</a></li>
                    <li data-target="focus-area"><a id="focus-area-link" href="#">Focus Area</a></li>
                    <li data-target="type"><a id="content-type-link" href="#">Content Type</a></li>
                    <li data-target="labs"><a id="labs-link" href="#">Lab</a></li>
                </ul>

            </div>
        </div>

        <div class="secondary-filter row">
            <div class="wrapper">
                <p class="filter-list" style="display:inline-block; color:#bbb;">Please select a filter category above</p>
                <ul class="filter-list" id="application">
                    <?php

                        $termlist = get_terms('application', array('hide_empty'=>0));
                        foreach($termlist as $term){
                            $class = '';
                            $slug = $term->taxonomy . "|" . $term->term_id . "|" . $term->name;
                            $id = $term->taxonomy . "_" . $term->term_id;
                            if(in_array($slug, $filters)) $class = 'hidden';
                            echo '<li data-id="'.$id.'" class="'.$class.'"><a href="#" data-slug="'.$slug.'">' . $term->name . '</a></li>';
                        }

                    ?>
                </ul>

                // ...two more of these category filters here...

                <ul class="filter-list" id="type">
                    <?php
                        global $post_icons;
                        $termlist = $post_icons;
                        foreach($termlist as $term=>$icon){
                            if($term == "") continue;
                            $class = '';
                            $slug = "type|" . $term;
                            $id = "type_" . $term;
                            if(in_array($slug, $filters)) $class = 'hidden';
                            echo '<li data-id="'.$id.'" class="'.$class.'"><a href="#" data-slug="'.$slug.'">' . ucfirst($term) . '</a></li>';
                        }
                    ?>
                </ul>

            </div>
        </div>

        <?php

            // show last-selected filter after submit

        ?>

        <div class="selection row">
            <div class="wrapper">
                <span class="label">APPLIED FILTERS  (click to remove)</span>

                <ul class="filter-list selections">
                    <?php
                    if(!empty($filters)):
                    foreach($filters as $f):
                        $f = explode('|', $f);
                        if($f[2]) {
                            $text = $f[2];
                            $id = $f[0] . "_" . $f[1];
                            $slug = $f[0] . "|" . $f[1] . "|" . $f[2];
                        } else {
                            $text = $f[1];
                            $id = "type_" . $f[1];
                            $slug = "type|" . $f[1];
                        }
                        echo '<li data-id="'.$id.'" class=""><a href="#" data-slug="'.$slug.'">'.ucwords($text).'</a></li>';
                    endforeach;
                endif;
                    ?>
                </ul>
            </div>
        </div>

    </div>
<?php endif; ?>

以下是他们抛出的错误:

尝试在/var/www/html/wp-content/themes/mytheme/functions.php中获取非对象的属性

0 个答案:

没有答案