Skip to content

Instantly share code, notes, and snippets.

@patilvikasj
Last active December 20, 2019 10:49
Show Gist options
  • Select an option

  • Save patilvikasj/abc64e8b2bd2aeb3eee95fb11be3c78c to your computer and use it in GitHub Desktop.

Select an option

Save patilvikasj/abc64e8b2bd2aeb3eee95fb11be3c78c to your computer and use it in GitHub Desktop.
Display primary category on shop page
<?php // Do not include this line in your theme's functions.php.
add_filter( 'astra_woo_shop_product_categories', 'display_primary_category_on_shop', 10, 2 );
function display_primary_category_on_shop( $categories_html, $product_id ) {
$terms = get_the_terms( $product_id, 'product_cat' );
if( ! empty( $terms ) ) {
$primary_category = get_post_meta( $product_id, '_yoast_wpseo_primary_product_cat', true );
foreach( $terms as $term ) {
if( $term->term_id == $primary_category ) {
return $term->name;
}
}
}
return $categories_html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment