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 );
    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">
            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(
                            'a' => array(
                                'href' => array(),
                                'rel'  => array(),
            } else {
                echo wc_get_product_category_list( $product->get_id(), ', ' );
    <?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>
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:

postmeta field

Dan 9 months 2022-09-01T21:57:45-05:00 0 Answers 0 views 0

Leave an answer