我已经搜索了数周,但仍然没有找到解决此问题的合适方法。
我正在写一个WordPress主题。我有一个称为Works的自定义帖子类型。我想将我的Works文件添加到我的菜单中,并在访问它们时突出显示其帖子。
我可以通过以下链接访问我的文件和帖子
作品存档:/works/
作品单篇文章:/works/postname/
到目前为止,我的解决方案是使用模板名称(工作文件)命名archive-works.php
模板文件。然后使用该模板创建一个空白页面,并将该页面添加到菜单中。这将突出显示菜单中的文件,而不是单个帖子。
我可以使用自定义链接和一些JavaScript轻松解决此问题,但必须有一种更好,更简洁的方法。
最佳答案
您可以在functions.php中做一个简单的技巧:
add_filter('nav_menu_css_class', 'current_type_nav_class', 10, 2);
function current_type_nav_class($classes, $item) {
// Get post_type for this post
$post_type = get_query_var('post_type');
// Go to Menus and add a menu class named: {custom-post-type}-menu-item
// This adds a 'current_page_parent' class to the parent menu item
if( in_array( $post_type.'-menu-item', $classes ) )
array_push($classes, 'current_page_parent');
return $classes;
}
在您的情况下,您只需在管理面板中通过该存档菜单项添加'works-menu-item'类;
关于wordpress - 如何将自定义帖子类型存档添加到菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20879401/