Php 如何更改Wordpress walker菜单中子菜单锚定的输出?
我将如何剥离显示在子菜单中的锚?它们只应围绕顶层的锚,但它会添加到所有锚,不幸的是,如下所示:Php 如何更改Wordpress walker菜单中子菜单锚定的输出?,php,wordpress,drop-down-menu,Php,Wordpress,Drop Down Menu,我将如何剥离显示在子菜单中的锚?它们只应围绕顶层的锚,但它会添加到所有锚,不幸的是,如下所示: <ul class="menu"> <li> <h2><a>First Item</a><h2> <ul class="sub-menu"> <li><h2><a>Sub item</a><h2>
<ul class="menu">
<li>
<h2><a>First Item</a><h2>
<ul class="sub-menu">
<li><h2><a>Sub item</a><h2><li> //<----- h2 tag, bad. :(
<ul>
<li>
</ul>
-
'
. $args->link\u之后
. $描述
. $args->after;
//由于$output是通过引用调用的,所以我们不需要返回任何内容。
$output.=应用过滤器(
“步行者导航菜单开始”
,$item_输出
,$item
,$depth
,$args
);
}
}
试试这个,基本上如果在类名中找到子菜单,它会去除h2,否则会保留它
$pos = strpos($class_names, "sub-menu");
if ($pos === false) {
$item_output = $args->before
. "<h2><a $attributes>"
. $args->link_before
. $title
. '</a></h2>'
. $args->link_after
. $description
. $args->after;
} else {
$item_output = $args->before
. "<a $attributes>"
. $args->link_before
. $title
. '</a>'
. $args->link_after
. $description
. $args->after;
}
$pos=strpos($class_名称,“子菜单”);
如果($pos==false){
$item_output=$args->before
. ""
.$args->link_之前
$title
. ''
.$args->link\u之后
.$description
.$args->after;
}否则{
$item_output=$args->before
. ""
.$args->link_之前
$title
. ''
.$args->link\u之后
.$description
.$args->after;
}
这也会从顶层锚固件中剥离锚固件。
class Description_Walker extends Walker_Nav_Menu
{
/**
* Start the element output.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. May be used for padding.
* @param array $args Additional strings.
* @return void
*/
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 ) . '"';
$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 ) .'"';
// insert description for top level elements only
// you may change this
$description = ( ! empty ( $item->description ) and 0 == $depth )
? '<small class="nav_desc">' . esc_attr( $item->description ) . '</small>' : '';
$title = apply_filters( 'the_title', $item->title, $item->ID );
$item_output = $args->before
. "<h2><a $attributes>"
. $args->link_before
. $title
. '</a></h2>'
. $args->link_after
. $description
. $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
);
}
}
$pos = strpos($class_names, "sub-menu");
if ($pos === false) {
$item_output = $args->before
. "<h2><a $attributes>"
. $args->link_before
. $title
. '</a></h2>'
. $args->link_after
. $description
. $args->after;
} else {
$item_output = $args->before
. "<a $attributes>"
. $args->link_before
. $title
. '</a>'
. $args->link_after
. $description
. $args->after;
}