admin管理员组文章数量:1434972
I need the Wordpress menu to include the 'current-menu-ancestor' class to reflect that site is currently in the recipe section. Supposing I have a recipe custom post type. I have the following code in my functions.php but it's not working:
function add_active_item_classes($classes = array(), $menu_item = false){
if ( get_post_type() == 'recipe' && $menu_item->title == 'Recipes') {
$classes[] = 'current-menu-ancestor';
return $menuclasses;
}
}
Also I don't know what filter hooks I will use to have this effect? Thanks for your suggestion and assistance.
I need the Wordpress menu to include the 'current-menu-ancestor' class to reflect that site is currently in the recipe section. Supposing I have a recipe custom post type. I have the following code in my functions.php but it's not working:
function add_active_item_classes($classes = array(), $menu_item = false){
if ( get_post_type() == 'recipe' && $menu_item->title == 'Recipes') {
$classes[] = 'current-menu-ancestor';
return $menuclasses;
}
}
Also I don't know what filter hooks I will use to have this effect? Thanks for your suggestion and assistance.
Share Improve this question asked Jan 2, 2013 at 2:27 Emerson ManingoEmerson Maningo 6192 gold badges12 silver badges29 bronze badges2 Answers
Reset to default 1This is the final working code:
<?php
function additional_active_item_classes($classes = array(), $menu_item = false){
global $wp_query;
if(in_array('current-menu-item', $menu_item->classes)){
$classes[] = 'current-menu-item';
}
if ( $menu_item->post_name == 'product' && is_post_type_archive('product') ) {
$classes[] = 'current-menu-item';
}
if ( $menu_item->post_name == 'product' && is_singular('product') ) {
$classes[] = 'current-menu-item';
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'additional_active_item_classes', 10, 2 );
?>
This code add class 'current-menu-ancestor' to parent item menu of your child CPT or custom taxonomy or default single post, in case you do not have a nested menu structure in the admin panel - only if you have 'level 0' menu. For example - if you have Page Product, which display products grid and you go to the single product - WP will not see the parent menu item. The code below improve this:
function additional_active_item_classes( $classes = array(), $menu_item = false ) {
// custom taxonomy
if ( $menu_item->title == 'Custom Tax Name Page' && is_tax('custom_tax') ) {
$classes[] = 'current-menu-ancestor';
}
// custom post type single
if ( $menu_item->title == 'Custom Post Type Page' && is_singular('products') ) {
$classes[] = 'current-menu-ancestor';
}
// blog post single
if ( $menu_item->title == 'Blog Page' && is_singular('post') ) {
$classes[] = 'current-menu-ancestor';
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'additional_active_item_classes', 10, 2 );
本文标签: How to include the 39currentmenuancestor39 class on a custom post type menu in Wordpress
版权声明:本文标题:How to include the 'current-menu-ancestor' class on a custom post type menu in Wordpress? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745624206a2666870.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论