Want to add a class to product category link if in current product category.
-
I am trying to add a class to a product category link, if in that category. Here’s the code I have so far.
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>"; } }
Someone else had suggested the following but it’s not working for me:
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>"; } }
Viewing 9 replies - 1 through 9 (of 9 total)
Viewing 9 replies - 1 through 9 (of 9 total)
- The topic ‘Want to add a class to product category link if in current product category.’ is closed to new replies.