Skip to content

Instantly share code, notes, and snippets.

@zonay
Created March 24, 2020 16:11
Show Gist options
  • Save zonay/4e2bfd4e32693b992728b19d6df1efba to your computer and use it in GitHub Desktop.
Save zonay/4e2bfd4e32693b992728b19d6df1efba to your computer and use it in GitHub Desktop.
Output a list of taxonomy terms, then highlight the terms the belong to the current post. #snippet #WordPress #woocommerce
<?php
$terms = get_the_terms( $post->ID, 'product_cat' );
if ( !empty( $terms ) ){
// get the first term
$term = array_shift( $terms );
$slugofpost = $term->slug;
}
?>
<style>
.shop-top-categories-menu .<?php echo $slugofpost; ?> {
background: #F2F2F2;
}
.shop-top-categories-menu .<?php echo $slugofpost; ?> a{
text-decoration: underline;
font-weight:bolder;
}
</style>
<?php
$tags = get_terms('product_cat', array(
'parent' => get_term_by('slug', $parent_term, $taxonomy)->term_id,
'hierarchical' => false,
'hide_empty' => 1,
'exclude' => 15, // excluding the 'uncategorized' category
));
$post_tags = wp_get_post_terms($post->ID,'product_cat',array('fields' => 'slugs'));
?>
<?php foreach( $tags as $tag ): ?>
<li class="<?php echo $tag->slug; ?> button d-inline-block text px-3 top-category-menu-item d-inline-flex">
<a href="<?php echo site_url().'/product-category/'.$tag->slug;?>" class="align-self-center"><?php echo $tag->name ?></a>
</li>
<?php endforeach;
wp_reset_query();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment