我到处找遍了,但似乎找不到这个具体案例的答案。我试图在子菜单开始后立即输出页面的父描述和特征图像。我有以下代码可以工作,但会迭代每个链接,这很混乱。我至少能够修改深度,使其只输出一次内容,但我想a)清理它,b)找到真正正确的方法来执行此操作。这是我现在的步行者:
class Description_Walker extends Walker_Nav_Menu {
function start_el(&$output, $item, $depth, $args) {
$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 ) . \'"\';
// Build default menu items
$output .= "<li id=\'menu-item-$item->ID\' $class_names>";
$attributes = \'\';
! empty( $item->attr_title )
and $attributes .= \' title="\' . esc_attr( $item->attr_title ) .\'"\';
! empty( $item->target )
and $attributes .= \' target="\' . esc_attr( $item->target ) .\'"\';
! empty( $item->xfn )
and $attributes .= \' rel="\' . esc_attr( $item->xfn ) .\'"\';
! empty( $item->url )
and $attributes .= \' href="\' . esc_attr( $item->url ) .\'"\';
// get thumbnail
$thumbnail = \'\';
if ( has_post_thumbnail( $item->object_id ) and 0 == $depth ) {
$thumbnail = get_the_post_thumbnail( $item->object_id );
}
// Build the description (you may need to change the depth to 0, 1, or 2)
$description = ( ! empty ( $item->description ) and 2 == $depth )
? \'<span class="nav_desc">\'. $item->description . \'</span>\' : \'\';
$title = apply_filters( \'the_title\', $item->title, $item->ID );
$item_output .= $args->before;
$item_output .= \'<a\'. $attributes .\'>\';
$item_output .= $args->link_before . apply_filters( \'the_title\', $item->title, $item->ID ) . $args->link_after;
$item_output .= \'</a>\';
// Only apply to the top level links, do not iterate over any sub-menu items:
if ( 0 == $depth ) {
$item_output .= "\\n$indent<div class=\\"dropdown\\"><div class=\\"container\\"><div class=\\"thumbnail\\">".$thumbnail."</div><div class=\\"nav-info\\">" . $item->description . "</div>\\n";
}
$item_output .= $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);
}
function end_lvl(&$output, $depth) {
$indent = str_repeat("\\t", $depth);
$output .= "\\n$indent</ul></div>";
}
}
编辑:
下面的答案是,我能够弄明白这一点。更正上述代码。这不是理想的情况,我真的只是想在LI之后立即输出内容,但最终我实际上需要额外的标记,我正在为以后需要做的样式注入这些标记。
我欢迎对此提出任何批评,因为我仍然觉得它很凌乱,但达格·纳比特,它是有效的!