[Resolved] Woocommerce / Display Category Name above Product Titel

Home Forums Support Woocommerce / Display Category Name above Product Titel

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1054451
    Kim

    Hi there, it would be nice to have an option to display the respective category name above each product on the shop archive pages and/or the single product pages. Some themes, like Flatsome for example, provide this option.

    Best regards!

    #1054641
    David
    Staff
    Customer Support

    Hi there,

    thats interesting. Something we can consider.
    You can do this with a Hook Element.
    This is the PHP required to display the first category:

    <?php 
    $terms = get_the_terms( get_the_ID(), 'product_cat' );
    if ( $terms && ! is_wp_error( $terms ) ) :
        if ( ! empty( $terms ) ) {
            echo $terms[0]->name;
        }?>
    <?php endif;?>

    The Hook would be the:
    woocommerce_after_shop_loop_item_title

    I think the review stars are hooked in at 5 so the priority would need to be 4 or lower.

    #1055517
    Kim

    Hi David, works perfectly 🙂 But how can I style this?

    #1055614
    David
    Staff
    Customer Support

    Change this:

    echo $terms[0]->name;

    to:

    echo '<span class="woo-custom-category">' . $terms[0]->name . '</span>';

    You can then style it with the .woo-custom-category CSS class.

    #1056532
    Kim

    Very nice! Thank you 🙂

    #1056729
    David
    Staff
    Customer Support
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.