好吧,这就是我最终解决它的原因。请注意,它并没有经过严格的测试,但至少它似乎可以工作。此外,如果您没有使用静态首页,则可能需要进行一些更改(尤其是get\\u选项(“page\\u for\\u posts”)。
还请注意,我使用“active parent”作为我的类名。这是因为我还使用了root的菜单清理代码,它将所有“current\\u page”类替换为“active”、“active parent”和“active祖先”。您可能想改用“官方”WP类名。
<?php
add_filter(\'nav_menu_css_class\', \'sleek_active_archive_link_on_taxonomies\', 10, 2);
function sleek_active_archive_link_on_taxonomies ($cssClasses, $item) {
global $wp_query;
# Only do this on archive pages
if (is_archive()) {
# This is the link to the blog archive
if ($item->object_id == get_option(\'page_for_posts\')) {
# If we\'re on a blog archive - give the blog link the active class
if (is_category() or is_tag() or is_day() or is_month() or is_year()) {
$cssClasses[] = \'active-parent\';
}
}
# This is a link to a custom post type archive
elseif ($item->type == \'post_type_archive\') {
# If we\'re on a taxonomy and this post type has that taxonomy - make it look active
if (is_tax()) {
global $wp_taxonomies;
$term = $wp_query->get_queried_object();
if (isset($wp_taxonomies[$term->taxonomy])) {
if (in_array($item->object, $wp_taxonomies[$term->taxonomy]->object_type)) {
$cssClasses[] = \'active-parent\';
}
}
}
}
}
return $cssClasses;
}
?>