一种方法是使用get_queried_object_id()
并将其与foreach循环中的类别术语ID进行比较。例如。
$current_object_id = get_queried_object_id();
foreach (get_categories() as $category) {
$classes = \'\';
if ( $category->term_id === $current_object_id ) {
$classes .= \'selected\';
}
printf(
\'<a class="%s" href="%s">%s</a>\',
$classes,
get_category_link($category->term_id),
$category->name
);
}
is_category()
应该工作太多,因为它基本上是一个花哨的包装为相同的检查如上所述。
foreach (get_categories() as $category) {
$classes = \'\';
// Parameter can be category ID, name, slug, or array of such
if ( is_category( $category->term_id ) ) {
$classes .= \'selected\';
}
printf(
\'<a class="%s" href="%s">%s</a>\',
$classes,
get_category_link($category->term_id),
$category->name
);
}