我有一个自定义导航助行器,我试图使用它只输出菜单的第一级:
<?php wp_nav_menu( array(
\'theme_location\' => \'secondary-menu\',
\'depth\' => 1,
\'walker\' => new SecondaryNavWalker
) ); ?>
然而,所有级别的导航都会输出,而不管输出结果如何。我试着检查一下
start_el
, 但是
depth
即使页面有父级,传入的参数也始终为零。我错过了什么?
以下是全定制导航助行器:
class SecondaryNavWalker extends Walker_Nav_Menu
{
function start_el(&$output, $item, $depth, $args)
{
//var_dump($item);
//var_dump(\'Args\');
//var_dump($args);
//var_dump(\'depth comp\');
//var_dump($depth);
//var_dump($args[\'depth\']);
if (get_post_meta( $item->ID, \'saltIsInMainNav\', true ) || $depth > $args[\'depth\']) {
return;
}
$classes = empty ( $item->classes ) ? array () : (array) $item->classes;
$class_names = join(
\' \'
, apply_filters(
\'nav_menu_css_class\'
, array_filter( $classes ), $item
)
);
! empty ( $class_names )
and $class_names = \' class="\'. esc_attr( $class_names ) . \'"\';
$output .= "<li id=\'menu-item-$item->ID\' $class_names>";
$attributes = \'\';
! empty( $item->post_title )
and $attributes .= \' title="\' . esc_attr( $item->post_title ) .\'"\';
! empty( $item->guid )
and $attributes .= \' href="\' . esc_url( site_url(\'/?p=\'.$item->ID) ) .\'"\';
$title = apply_filters( \'the_title\', $item->post_title, $item->ID );
$item_output = $args->before
. "<a $attributes>"
. $args->link_before
. "$title"
. $args->link_after
. \'</a> \'
. $args->after;
// Since $output is called by reference we don\'t need to return anything.
$output .= apply_filters(
\'walker_nav_menu_start_el\'
, $item_output
, $item
, $depth
, $args
);
}
}
未注释时,这些深度var\\u转储输出以下内容:
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
EDIT: @聊天室里的Howdy\\u McGee发给我的
this code 在游戏中,他成功地使用了
start_el
方法这段代码对我不起作用,因为它引用的许多属性(->类、->属性标题、->目标、->xfn、->url)没有包含在我获取的WP\\U Post对象中。这怎么可能?
EDIT 2: Here\'s the transcript 在我与“Howdy\\u McGee”合作的车队中,我们缩小了问题范围,但未能找到解决方案。