Home / eCommerce / Show Product Categories in the Vendor Product Catalog
Duplicate Snippet

Embed Snippet on Your Site

Show Product Categories in the Vendor Product Catalog

This PHP code snippet automatically detects any page or post that contains the [wcv_products] shortcode and will display the product categories above the product titles wherever the shortcode is used.

Code Preview
php
<?php
add_action( 'woocommerce_shop_loop_item_title', 'add_category_above_product_title', 6 );
function add_category_above_product_title() {
    global $product;
    $current_post_id = isset( $GLOBALS['wp_query']->queried_object_id ) ? $GLOBALS['wp_query']->queried_object_id : 0;
    if ( ! $current_post_id ) {
        return;
    }
    $post_content = get_post_field( 'post_content', $current_post_id );
    if (
        strpos( $post_content, '[wcv_products' ) === false
    ) {
        return;
    }
    $categories = wc_get_product_category_list(
        $product->get_id(),
        ', ',
        '<div class="product-categories">',
        '</div><br>'
    );
    echo $categories;
}

Comments

Add a Comment