categories – Function to reference post meta in place of category Woocommerce
I recently installed a plugin for wordpress/woocommerce that allows you to choose a primary category for a product when it’s in multiple categories. It works well to change the breadcrumbs, but my theme (peakshops) also displays the primary category under the corresponding thumbnails on the the shop page.
This is the core function that is built into the theme to display the category (and link to the category) under the thumbnail:
// Add Title with Link.
function thb_template_loop_product_title() {
global $product;
$product_url = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
?>
<?php
if ( 'on' === ot_get_option( 'shop_product_listing_category', 'on' ) ) {
$shop_product_listing_category_single = ot_get_option( 'shop_product_listing_category_single', 'on' );
?>
<div class="product-category">
<?php
if ( 'on' === $shop_product_listing_category_single ) {
$product_cats = wc_get_product_category_list( get_the_ID(), '\n', '', '' );
if ( $product_cats ) {
list( $first_part ) = explode( '\n', $product_cats );
echo wp_kses(
$first_part,
array(
'a' => array(
'href' => array(),
'rel' => array(),
),
)
);
}
} else {
echo wc_get_product_category_list( $product->get_id(), ', ' );
}
?>
</div>
<?php } ?>
<h2 class="<?php echo esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ); ?>"><a href="<?php echo esc_url( $product_url ); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php
}
add_action( 'woocommerce_shop_loop_item_title', 'thb_template_loop_product_title', 10 );
This core function seems to grab the first category rather than the primary category assigned by the plugin. Is there a way to change this action (or remove it and create a new one) so that it references the primary category assigned. For reference, the primary category plugin stores the value as a post meta field like this:
Leave an answer