好的,这两个答案都不是很优雅,因为您必须手动输入CPTs名称。下面是一个简单的插入片段。它几乎是从here 这是该网站的荣誉。
This snippet removes the current_page_parent
class of the blog menu item:
add_filter( \'nav_menu_css_class\', \'theme_remove_cpt_blog_class\', 10, 3 );
function theme_remove_cpt_blog_class( $classes, $item, $args ) {
if( !is_singular( \'post\' ) AND !is_category() AND !is_tag() AND !is_date() ):
$blog_page_id = intval( get_option( \'page_for_posts\' ) );
if( $blog_page_id != 0 AND $item->object_id == $blog_page_id )
unset( $classes[ array_search( \'current_page_parent\', $classes ) ] );
endif;
return $classes;
}
虽然主要不是问这个问题,但我认为在大多数情况下,当从博客菜单项中删除类时,您希望一个类突出显示CPTs存档菜单项,所以这里也有一个片段。
This snippet adds a current_page_parent
class on the archive menu item of a CPT:
add_action( \'nav_menu_css_class\', \'theme_add_cpt_ancestor_class\', 10, 3 );
function theme_add_cpt_ancestor_class( $classes, $item, $args ) {
global $post;
$current_post_type = get_post_type_object( get_post_type( $post->ID ) );
$current_post_type_slug = $current_post_type->rewrite[ \'slug\' ];
$menu_slug = strtolower( trim( $item->url ) );
if( strpos( $menu_slug, $current_post_type_slug ) !== false )
$classes[] = \'current_page_parent\';
return $classes;
}