0

I want to display woocommerce single item category in shop loop. And i am using hooks action. Here is my code:

<?php 
remove_action('woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10);

function loop_title() { ?>
    <div class="col-xl-3 col-md-3 col-sm-3">
        <h3><a href="<?php the_permalink(); ?>" class="feed-item-baslik"><?php the_title(); ?></a></h3>
        <?php 
        global $post;
        $postcat = get_the_category( $term_id );

        if ( ! empty( $postcat ) ) {
            echo esc_html( $postcat[0]->name );   
        }
        ?>
    </div>
<?php }
add_action('woocommerce_shop_loop_item_title', 'loop_title', 10);
?>

But it not displays category name. And i also tried get_queried_object and get_term.

1
<?php remove_action('woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10);
function loop_title() { 
global $post;?>
<div class="col-xl-3 col-md-3 col-sm-3">
    <h3><a href="<?php the_permalink(); ?>" class="feed-item-baslik"><?php the_title(); ?></a></h3>
    <?php 
    $terms = get_the_terms( $post->ID, 'product_cat' );
    if ( $terms && ! is_wp_error( $terms ) ) :
        if ( ! empty( $terms ) ) {
            echo $terms[0]->name;
        }?>
    <?php endif;?>
</div>
<?php }
add_action('woocommerce_shop_loop_item_title', 'loop_title', 10); ?>

Replaced get_the_category() with get_the_terms(). Hope this helps !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.