Within Shoptimizer’s ‘Product cards’ (on your catalog or product listings pages) there is an option by default to display the product categories within which the item belongs. If the item is within say two categories, this will typically look fine. But it can look unwieldy and messy if it five or six categories are displayed in this area.
It may be nice to show only a ‘Primary product category’ in this space.
WordPress includes a ‘Primary category’ setting for Blog post categories. But this core functionality is missing within WooCommerce’s product categories.
1. Install Yoast, Rank Math, or AIOSEO
Popular SEO plugins Yoast SEO, Rank Math and AIOSEO include this functionality. Install one of these. You could also separately check out our Rank Math review.
Now, when you edit an item’s product categories you’ll be able to set a Primary product category or term.
2. Add a code snippet
Next, within your child theme’s functions.php file or via the Code Snippets plugin you’ll need to include a custom function.
If using Yoast SEO:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
add_action( 'wp', 'remove_default_shoptimizer_product_title', 99 ); function remove_default_shoptimizer_product_title() { remove_action( 'woocommerce_shop_loop_item_title', 'shoptimizer_loop_product_title', 10 ); } add_action( 'woocommerce_shop_loop_item_title', 'shoptimizer_loop_yoast_product_title', 10 ); function shoptimizer_loop_yoast_product_title() { if( class_exists( 'WPSEO_Options' ) ) { global $post, $woocommerce, $product; $primary_cat_id = get_post_meta($product->id,'_yoast_wpseo_primary_product_cat',true); if( $primary_cat_id ) { $product_cat = get_term($primary_cat_id, 'product_cat'); if( isset( $product_cat->name ) ) $category_link = get_category_link( $primary_cat_id ); echo '<p class="product__categories"><a href = "'.esc_url( $category_link ).'" class="catName">'.$product_cat->name.'</a></p>'; echo '<div class="woocommerce-loop-product__title"><a href="' . get_the_permalink() . '" aria-label="' . get_the_title() . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">' . get_the_title() . '</a></div>'; } else { echo '<p class="product__categories">' . wc_get_product_category_list( get_the_id(), ', ', '', '' ) . '</p>'; echo '<div class="woocommerce-loop-product__title"><a href="' . get_the_permalink() . '" aria-label="' . get_the_title() . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">' . get_the_title() . '</a></div>'; } } } |
If using Rank Math:
Add this snippet to your child theme’s functions.php file or via the Code Snippets plugin.
1 2 3 |
add_filter( 'term_links-product_cat', function( $links ) { return [ $links[0] ]; }); |
If using AIOSEO:
Add this snippet to your child theme’s functions.php file or via the Code Snippets plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
add_action( 'woocommerce_shop_loop_item_title', 'shoptimizer_loop_aioseo_product_title', 10 ); function shoptimizer_loop_aioseo_product_title() { if ( ! function_exists( 'aioseo' ) ) { return; } global $product; remove_action( 'woocommerce_shop_loop_item_title', 'shoptimizer_loop_product_title', 10 ); // Get the AIOSEO primary term for the current product. $primary_cat = aioseo()->standalone->primaryTerm->getPrimaryTerm( $product->get_id(), 'product_cat' ); if ( is_a( $primary_cat, 'WP_Term' ) ) { if( isset( $product_cat->name ) ) { $category_link = get_category_link( $primary_cat_id ); echo '<p class="product__categories"><a href = "'.esc_url( $category_link ).'" class="catName">'.$product_cat->name.'</a></p>'; echo '<div class="woocommerce-loop-product__title"><a href="' . get_the_permalink() . '" aria-label="' . get_the_title() . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">' . get_the_title() . '</a></div>'; } } else { echo '<p class="product__categories">' . wc_get_product_category_list( $product->get_id(), ', ', '', '' ) . '</p>'; echo '<div class="woocommerce-loop-product__title"><a href="' . get_the_permalink() . '" aria-label="' . get_the_title() . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">' . get_the_title() . '</a></div>'; } } |