我正在研究一个主题,该主题需要在导航中嵌套一段动态内容。我正在使用自定义的walker生成菜单。我需要一种向代码中添加标记的方法,该方法将在后端进行编辑(可能是ACF,不重要)。
内容将嵌套在菜单的特定部分(“产品”)。不知道如何使用定制助行器来实现这一点。
步行者:
class Custom_Nav_Walker extends Walker_Nav_Menu {
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$default_classes = empty ( $item->classes ) ? array () : (array) $item->classes;
$custom_classes = (array)get_post_meta( $item->ID, \'_menu_item_classes\', true );
// Is this a top-level menu item?
if ($depth == 0)
$custom_classes[] = \'menu-item-top-level\';
// Does this menu item have children?
if (in_array(\'menu-item-has-children\', $default_classes))
$custom_classes[] = \'menu-item-has-children\';
// Is this menu item active? (Top level only)
$active_classes = array(\'current-menu-item\', \'current-menu-parent\', \'current-menu-ancestor\', \'current_page_item\', \'current-page-parent\', \'current-page-ancestor\');
if ($depth == 0 && array_intersect($default_classes, $active_classes))
$custom_classes[] = \'menu-item-active\';
// Give menu item a class based on its level/depth
$level = $depth + 1;
if ($depth > 0)
$custom_classes[] = "menu-item-level-$level";
$classes = join(\' \', $custom_classes);
! empty ( $classes )
and $classes = \' class="\'. trim(esc_attr( $classes )) . \'"\';
$output .= "<li $classes>";
$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 ) .\'"\';
$title = apply_filters( \'the_title\', $item->title, $item->ID );
$item_output = $args->before
. "<a $attributes>"
. $args->link_before
. $title
. \'</a> \'
. $args->link_after
. $description
. $args->after;
$output .= apply_filters(
\'walker_nav_menu_start_el\'
, $item_output
, $item
, $depth
, $args
);
}
}
这在我的标记中调用如下:<nav>
<?php wp_nav_menu( array(\'menu\' => \'main\', \'walker\' => new Custom_Nav_Walker )); ?>
</nav>
我很感激你能提供的任何帮助。