我正在使用自定义贴子类型,但导航菜单无法获取.current-menu-item
在我的custom post archive page
菜单项。事实上,当我在那一页或任何custom posts
, 不是cpt archive菜单项获取当前类,而是指向文章的菜单项获取.current-menu-item
(我的博客页面)。
经过几个小时的google测试和黑客攻击,我发现了这个thread on wordpress 下面是经过编辑的解决方案修复版本的副本:
// As of WP 3.1.1 addition of classes for css styling to parents of custom post types doesn\'t exist.
// We want the correct classes added to the correct custom post type parent in the wp-nav-menu for css styling and highlighting, so we\'re modifying each individually...
// The id of each link is required for each one you want to modify
// Place this in your WordPress functions.php file
function remove_parent_classes($class)
{
// check for current page classes, return false if they exist.
return ($class == \'current_page_item\' || $class == \'current_page_parent\' || $class == \'current_page_ancestor\' || $class == \'current-menu-item\') ? FALSE : TRUE;
}
function add_class_to_wp_nav_menu($classes)
{
switch (get_post_type())
{
case \'artist\':
// we\'re viewing a custom post type, so remove the \'current_page_xxx and current-menu-item\' from all menu items.
$classes = array_filter($classes, "remove_parent_classes");
// add the current page class to a specific menu item (replace ###).
if (in_array(\'menu-item-171\', $classes))
{
$classes[] = \'current_page_parent\';
}
break;
}
return $classes;
}
add_filter(\'nav_menu_css_class\', \'add_class_to_wp_nav_menu\');
当我们使用菜单项id时,这个解决方案是有效的,但我发现它非常难看,不可能将其用于插件开发。。。当我们在存档页面或以更干净的方式在其中一个自定义帖子上时,有没有其他想法来选择与自定义帖子存档页面相对应的菜单项?
换句话说,归档模板不应该从框中选择自己的菜单项
我从来没有想过在wordpress导航菜单中突出某个元素可能是自定义帖子类型的问题,我对此有点失望。无论如何,下面是我用来管理自定义帖子(艺术家)的代码的n表:
artist.php:
/****************************************************************
* Custom Post Type
****************************************************************/
add_action( \'init\', \'custom_post_artist\' );
function custom_post_artist()
{
$labels = array(
[...]
);
register_post_type( \'artist\',
array(
\'labels\' => $labels,
\'public\' => true,
\'menu_position\' => 15,
\'supports\' => array( \'title\', \'editor\', \'comments\', \'thumbnail\', \'revisions\', \'excerpt\'),
\'show_in_nav_menus\' => true,
\'show_in_menu\' => true,
\'taxonomies\' => array( \'artist_genre\', \'artist_music_type\' ),
\'has_archive\' => \'artistes\',
\'rewrite\' => array( \'slug\' => __(\'artistes\', \'ppdev\'), \'with_front\' => False )
)
);
}
/****************************************************************
* Templates
****************************************************************/
add_filter( \'template_include\', \'include_tpl_function\', 1 );
function include_tpl_function( $template_path )
{
if ( get_post_type() == \'artist\' )
{
if ( is_single() )
{
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ( $theme_file = locate_template( array(\'single-artist.php\') ) )
{
$template_path = $theme_file;
}
else
{
$template_path = ARTIST_PATH . \'templates/single-artist.php\';
}
}
else if( is_archive() )
{
if ( $theme_file = locate_template( array(\'archive-artist.php\') ) )
{
$template_path = $theme_file;
}
else
{
$template_path = ARTIST_PATH . \'templates/archive-artist.php\';
}
}
}
return $template_path;
}
非常基本的存档模板页面:archive-artist.php:
<section id="primary">
<div id="content" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="page-title">Latest artists</h1>
</header>
<!-- Start the Loop -->
<?php while ( have_posts() ) : the_post();
$thumbnail_attr = array(
\'class\' => "aligncenter",
\'alt\' => get_the_title()
);
<h2>
<a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
if ( has_post_thumbnail() ) : ?>
<a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( \'full\', $thumbnail_attr ); ?>
</a>
<?php endif; ?>
<?php endwhile; ?>
<!-- Display page navigation -->
<?php global $wp_query;
if ( isset( $wp_query->max_num_pages ) && $wp_query->max_num_pages > 1 ) { ?>
<nav id="<?php echo $nav_id; ?>">
<div class="nav-previous"><?php next_posts_link( \'<span class="meta-nav">←</span> Previous artists\' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( \'Next artists <span class= "meta-nav">→</span>\' ); ?></div>
</nav>
<?php };
endif; ?>
</div>
</section>
注:也测试了十二个主题和同一期