使用nav_menu_css_class
要将类添加到的筛选器wp_nav_menu
输出
添加ID(无需其他查询):
function wpa_category_nav_class( $classes, $item ){
if( \'category\' == $item->object ){
$classes[] = \'menu-category-\' . $item->object_id;
}
return $classes;
}
add_filter( \'nav_menu_css_class\', \'wpa_category_nav_class\', 10, 2 );
添加slug(通过加载类别对象
get_category
):
function wpa_category_nav_class( $classes, $item ){
if( \'category\' == $item->object ){
$category = get_category( $item->object_id );
$classes[] = \'menu-\' . $category->slug;
}
return $classes;
}
add_filter( \'nav_menu_css_class\', \'wpa_category_nav_class\', 10, 2 );