我想知道如何在菜单链接中添加一个“活动”类。
例如,如果在Shirts类别中,我希望它有一个活动类,以便我可以使用不同的样式:
Shirts | 牛仔裤|鞋
以下是我当前使用的代码:
function show_parent_categories( $args = array() ) {
$args = array(
\'hierarchical\' => 1,
\'show_option_none\' => \'\',
\'hide_empty\' => 0,
\'parent\' => $category->term_id,
\'taxonomy\' => \'product_cat\'
);
$product_categories = get_terms( \'product_cat\', $args );
$count = count($product_categories);
if ( $count > 0 ){
echo "<div id=\'main-categories-wrapper\'><ul id=\'main-categories\'>";
foreach ( $product_categories as $product_category ) {
echo \'<li><a href="\' . get_term_link( $product_category ) . \'">\' . $product_category->name . \'</li>\';
}
echo "</ul></div>";
}
}
SO网友:tam
您可以使用is_category()
您可以查看文档here
function show_parent_categories( $args = array() ) {
$args = array(
\'hierarchical\' => 1,
\'show_option_none\' => \'\',
\'hide_empty\' => 0,
\'parent\' => $category->term_id,
\'taxonomy\' => \'product_cat\'
);
$product_categories = get_terms( \'product_cat\', $args );
$count = count($product_categories);
if ( $count > 0 ){
echo "<div id=\'main-categories-wrapper\'><ul id=\'main-categories\'>";
foreach ( $product_categories as $product_category ) {
echo \'<li><a class="\'.(is_category($product_category->slug)?\'active\':\'\').\'" href="\' . get_term_link( $product_category ) . \'">\' . $product_category->name . \'</li>\';
}
echo "</ul></div>";
}
}