我读过一篇关于如何将类应用于锚点而不是使用nav_menu_link_attributes
还有一个步行者。
但是,它们都没有实现菜单中要在锚上使用的自定义类选项。允许用户添加自己的类似乎是一个不错的特性,但是,如果我想将该类应用于主题中的特定孤立元素,该怎么办?
我试过了,但没有看到它在哪里记录了这个数据值返回的内容?如何引用此可选数据?我的项目使用anchor class属性滚动到页面的部分。
我读过一篇关于如何将类应用于锚点而不是使用nav_menu_link_attributes
还有一个步行者。
但是,它们都没有实现菜单中要在锚上使用的自定义类选项。允许用户添加自己的类似乎是一个不错的特性,但是,如果我想将该类应用于主题中的特定孤立元素,该怎么办?
我试过了,但没有看到它在哪里记录了这个数据值返回的内容?如何引用此可选数据?我的项目使用anchor class属性滚动到页面的部分。
您可以像这样访问这些类:
// where \'Top\' is the menu name, slug or ID. But not menu location.
$menu_items = wp_get_nav_menu_items( \'Top\' );
foreach ( $menu_items as $menu_item ) {
$menu_classes = $menu_item->classes;
print_r( $menu_classes );
}
我最终发现它是$items->classes
因此,使用描述助行器检查是否存在空$items->classes[0]
.
! empty( $item->classes[0] )
and $attributes .= \' class="\' . esc_attr( $item->classes[0] ) .\'"\';
对于那些希望在自己的项目中使用此功能的人,请使用下面的代码。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|object $args Additional strings. Actually always an
instance of stdClass. But this is WordPress.
* @return void
*/
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 )
{
$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 ) .\'"\';
//if the custom classes field is NOT empty dump it into the link attributes.
! empty( $item->classes[0] )
and $attributes .= \' class="\' . esc_attr( $item->classes[0] ) .\'"\';
// 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
. "<a $attributes>"
. $args->link_before
. $title
. \'</a> \'
. $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
);
}
}
如果这对你有帮助,请告诉我!我想用wp_list_categories 为了帮助我操作列表类,我希望在start_lvl 方法,所以我尝试使用$category->term_id 但是抛出了错误。现在我只想在start_lvl 方法来匹配我在start_el 输出,请在下面的类中检查此行$output .= \"<span id=\\\"$category->term_id\\\" class=\\\"toggle\\\" onClick=\\\"toggleMe(this.id);\\\" style=\\\"c