Skip to content

Instantly share code, notes, and snippets.

@guytzhak
Created June 8, 2020 20:32
Show Gist options
  • Save guytzhak/69cff7cca00f099fd584fd745e2e9683 to your computer and use it in GitHub Desktop.
Save guytzhak/69cff7cca00f099fd584fd745e2e9683 to your computer and use it in GitHub Desktop.
Display sub cats with "active" class for the active sub category
function mz_display_sub_cats() {
$object = get_queried_object();
$current = $object->term_id;
echo '<div class="cats cats_filters d-flex flex-nowrap">';
$class = '';
if( $object->parent > 0 ) {
$terms = get_term_children($object->parent, 'product_cat');
$object = get_term($object->parent, 'product_cat');
} else {
$terms = get_term_children($object->term_id, 'product_cat');
$class = 'active';
}
$link = get_term_link($object);
$all = __('הכל', 'cs');
echo "<div class='cats_filters-item {$class}'><a href='{$link}' title='{$all}'>{$all} <span class='count'>({$object->count})</span></a></div>";
if( !is_wp_error($terms) ) {
foreach ($terms as $term_id) {
$class = $current == $term_id ? 'active' : '';
$term = get_term($term_id, 'product_cat');
$link = get_term_link($term);
echo "<div class='cats_filters-item {$class}'><a href='{$link}' title='{$term->name}'>{$term->name} <span class='count'>({$term->count})</span></a></div>";
}
}
echo '</div>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment