好的,我已经使用了一个函数来实现这一点。我刚开始使用WordPress,所以我不确定这段代码的质量或效率。但这里是:-
function get_active_menu_item_ids( $classes )
{
//set up defaults for menu retrieval
$dosmenudefaults = array( \'menu\' => \'\', \'container\' => \'div\', \'container_class\' => \'\', \'container_id\' => \'\', \'menu_class\' => \'menu\', \'menu_id\' => \'\',
\'echo\' => true, \'fallback_cb\' => \'wp_page_menu\', \'before\' => \'\', \'after\' => \'\', \'link_before\' => \'\', \'link_after\' => \'\', \'items_wrap\' => \'<ul id="%1$s" class="%2$s">%3$s</ul>\',
\'depth\' => 0, \'walker\' => \'\', \'theme_location\' => \'\' );
$dosargs = wp_parse_args( $dosargs, $dosmenudefaults );
$dosargs = apply_filters( \'wp_nav_menu_args\', $dosargs );
$dosargs = (object) $dosargs;
// Get the nav menu based on the requested menu
$dosmenu = wp_get_nav_menu_object( $dosargs->menu );
// Get the nav menu based on the theme_location
if ( ! $dosmenu && $dosargs->theme_location && ( $doslocations = get_nav_menu_locations() ) && isset( $doslocations[ $dosargs->theme_location ] ) )
$dosmenu = wp_get_nav_menu_object( $doslocations[ $dosargs->theme_location ] );
// Get the first menu that has items if we still can\'t find a menu
if ( ! $dosmenu && !$dosargs->theme_location ) {
$dosmenus = wp_get_nav_menus();
foreach ( $dosmenus as $dosmenu_maybe ) {
if ( $dosmenu_items = wp_get_nav_menu_items($dosmenu_maybe->term_id) ) {
$dosmenu = $dosmenu_maybe;
break;
}
}
}
// If the menu exists, get its items.
if ( $dosmenu && ! is_wp_error($dosmenu) && !isset($dosmenu_items) )
$dosmenu_items = wp_get_nav_menu_items( $dosmenu->term_id );
$dosmenu = $dosmenu_items;
// Get the $menu_item variables
_wp_menu_item_classes_by_context( $dosmenu );
//create empty parents array
$dosparents = array();
//Iterate through the menu items and get the active item and its parent items
foreach ($dosmenu as $dosmenuitem)
{
if ($dosmenuitem->current == \'1\')
{
$dosactivemenuids[\'current\']= $dosmenuitem->ID;
}
if (($dosmenuitem->current_item_parent == \'1\')||($dosmenuitem->current_item_ancestor == \'1\'))
{
$dosparents[$dosmenuitem->menu_order] = $dosmenuitem->ID;
}
if(is_array($dosparents))
{
krsort($dosparents);
foreach ($dosparents as $key =>$value)
{
$dosactivemenuids[\'parents\'][$key]= $value;
}
}
}
//return $dosactivemenuids;
if (is_array($dosactivemenuids[\'parents\']))
{
foreach ( $dosactivemenuids[\'parents\'] as $key=>$value)
{
$classes[] = \'menu-item-\'.$value;
}
}
if ($dosactivemenuids[\'current\'] != \'\')
{
$classes[] = \'menu-item-\'.$dosactivemenuids[\'current\'];
}
return $classes;
}
您可以将其添加到模板的函数中。php’以及函数后面的某个地方添加一个过滤器来调用它:-
add_filter( \'body_class\', \'get_active_menu_item_ids\');
What this does?
这样做的目的是获取活动菜单ID,并按升序将其添加到body类中
Why would I need this?
来自CMS背景,我习惯了菜单定义的页面。使用此功能,活动菜单项id和/或其父项id将添加到body类中,以便您可以基于活动菜单项添加页面范围的样式。例如,您可能有如下菜单结构:-
->菜单项-1
->菜单项2
->->菜单项3
如果您正在查看“menu-item-3”页面,则使用此功能,类“menu-item-2”和“menu-item-3”将按此顺序添加到body标记中。这意味着您可以有一个针对body的css规则。menu-item-3可以返回到正文中的父项规则。菜单项-2,如果两者都没有样式,则最终为正文。例如,我使用它的目的是为整个页面(正文)设置默认背景色,当菜单项2处于活动状态时(正文。菜单项2)为整个页面设置背景色,当菜单项3处于活动状态时(正文。菜单项3)为页面设置不同的背景色。
希望能帮助别人;)