the_category()
使用get_the_category_list()
但此函数无法指定类。但是,您可以使用the_category
钩由于您知道当前类别链接的格式,因此可以对其进行搜索和替换。
它看起来像这样(未经测试):
add_filter(\'the_category\', \'highlight_current_cats\', 10, 3);
function highlight_current_cats($thelist, $separator, $parents)
{
// The current cat links will look like <a href="[category link]" [other stuff]
// We want them it look like <a href="[category link]" class="current-cat" [other stuff]
$current_cats = get_the_category();
if ($current_cats) {
foreach ($current_cats as $cat) {
$cat_link = get_category_link($cat->term_id);
$thelist = str_replace(\'<a href="\' . $cat_link . \'"\', \'<a href="\' . $cat_link . \'" class="current-cat"\', $thelist);
}
}
return $thelist;
}