admin管理员组文章数量:1023815
For a menu which has only top-level links to custom post type archive pages, how can I add the .active
class to the top-level item when viewing that post type's single?
There's lots of similar questions on WPSE (like this or this), but none seem to address my specific question and make use of .current-menu-item
or .current-menu-ancestor
(and associated) classes, which don't appear on my menu (as I assume because the single posts aren't part of the menu list structure).
The only way I can think to do this is check the current post type against the slug, but it feels a bit hacky (and doesn't allow for url rewrites, etc).
For a menu which has only top-level links to custom post type archive pages, how can I add the .active
class to the top-level item when viewing that post type's single?
There's lots of similar questions on WPSE (like this or this), but none seem to address my specific question and make use of .current-menu-item
or .current-menu-ancestor
(and associated) classes, which don't appear on my menu (as I assume because the single posts aren't part of the menu list structure).
The only way I can think to do this is check the current post type against the slug, but it feels a bit hacky (and doesn't allow for url rewrites, etc).
Share Improve this question edited Jan 28, 2019 at 9:44 mistertaylor asked Jan 24, 2019 at 10:40 mistertaylormistertaylor 6411 gold badge6 silver badges20 bronze badges2 Answers
Reset to default 0Take a look at this link, which will help you add the class for custom post types: https://gist.github/gerbenvandijk/5253921
Solved using an adapted version of one of the answers from @mrbenhenson's link.
function custom_active_item_classes($classes = array(), $menu_item = false){
global $post;
if ($menu_item->type == "post_type_archive" ) {
$classes[] = ($menu_item->url == get_post_type_archive_link($post->post_type)) ? 'current-menu-item active' : '';
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'custom_active_item_classes', 10, 2 );
For a menu which has only top-level links to custom post type archive pages, how can I add the .active
class to the top-level item when viewing that post type's single?
There's lots of similar questions on WPSE (like this or this), but none seem to address my specific question and make use of .current-menu-item
or .current-menu-ancestor
(and associated) classes, which don't appear on my menu (as I assume because the single posts aren't part of the menu list structure).
The only way I can think to do this is check the current post type against the slug, but it feels a bit hacky (and doesn't allow for url rewrites, etc).
For a menu which has only top-level links to custom post type archive pages, how can I add the .active
class to the top-level item when viewing that post type's single?
There's lots of similar questions on WPSE (like this or this), but none seem to address my specific question and make use of .current-menu-item
or .current-menu-ancestor
(and associated) classes, which don't appear on my menu (as I assume because the single posts aren't part of the menu list structure).
The only way I can think to do this is check the current post type against the slug, but it feels a bit hacky (and doesn't allow for url rewrites, etc).
Share Improve this question edited Jan 28, 2019 at 9:44 mistertaylor asked Jan 24, 2019 at 10:40 mistertaylormistertaylor 6411 gold badge6 silver badges20 bronze badges2 Answers
Reset to default 0Take a look at this link, which will help you add the class for custom post types: https://gist.github/gerbenvandijk/5253921
Solved using an adapted version of one of the answers from @mrbenhenson's link.
function custom_active_item_classes($classes = array(), $menu_item = false){
global $post;
if ($menu_item->type == "post_type_archive" ) {
$classes[] = ($menu_item->url == get_post_type_archive_link($post->post_type)) ? 'current-menu-item active' : '';
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'custom_active_item_classes', 10, 2 );
本文标签: filtersAdd active class to toplevel menu items when viewing single for that post type
版权声明:本文标题:filters - Add active class to top-level menu items when viewing single for that post type 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745527243a2154583.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论