Last active
December 20, 2019 10:49
-
-
Save patilvikasj/abc64e8b2bd2aeb3eee95fb11be3c78c to your computer and use it in GitHub Desktop.
Display primary category on shop page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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