Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tankbar/11fb10a6789509cf6e30de433e18c45a to your computer and use it in GitHub Desktop.
Save tankbar/11fb10a6789509cf6e30de433e18c45a to your computer and use it in GitHub Desktop.
Strip unwanted product categories in categories loop on single product page and/or woocommerce loop
//Strip unwanted product categories in categories loop on single product page and/or woocommerce loop
//Add this manually to your wanted hook or filter
function list_clean_categories() {
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
$unwanted_cat_slugs = array();
//Example: $unwanted_cat_slugs[] = "campaign-summer";
$unwanted_cat_slugs[] = "xxx";
$unwanted_cat_slugs[] = "yyy-yyy";
$term_count = count($terms);
$index = 1;
foreach ( $terms as $term ) {
$term_link = get_term_link( $term );
if(!in_array($term->slug, $unwanted_cat_slugs)){
// We don't show a comma on the last index.
if ($index < $term_count) {
echo ' <a href="' . esc_url( $term_link ) . '">' . $term->name . ', </a>';
}
else {
echo ' <a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>';
}
}
$index++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment