Почему WordPress wp_nav_menu пусто в search.php?
По какой-то причине мой бродяга в Wordpress выплевывает пустые элементы меню, но только на странице search.php. Вот пример вывода:
<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>
А вот код, который генерирует HTML:
<nav class="main-nav" role="navigation">
<?php
wp_nav_menu(array(
'menu' => 'Main Menu',
'container_id' => 'cssmenu',
'walker' => new CSS_Menu_Maker_Walker()
));
?>
</nav>
А вот код nav walker в functions.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";
}
}
Итак, опять же, это прекрасно работает на каждой странице, кроме search.php. Единственное, о чем я могу думать, это то, что, возможно, этот код из header.php (который, конечно, вызывается search.php) как-то конфликтует с пространством имен:
<?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; ?>
У кого-нибудь когда-нибудь такое случалось? Ты видишь, чего мне не хватает?
РЕДАКТИРОВАТЬ Добавлен код после is_search()
Итак, я наконец-то получил отладку, работающую на сервере разработки (поиск не работает на моем локальном компьютере), и похоже, что эти две строки вызывают ошибку (ближе к концу кода nav walker):
>> $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
>> $item_output .= $args->after;
Вот ошибка, которую они бросают:
Попытка получить свойство необъекта в /var/www/html/wp-content/themes/mytheme/functions.php