[Resolved] Woocommerce / Display Category Name above Product Titel

Home Forums Support [Resolved] Woocommerce / Display Category Name above Product Titel

Home Forums Support Woocommerce / Display Category Name above Product Titel

Viewing 8 posts - 1 through 8 (of 8 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

    Happy to be of help

    #1668490
    Pedro

    Hola David,

    He agregado este hook, sin embargo no muestra el nombre de la categorĂ­a.

    Este hook es para mostrar la categoría en la tarjeta del producto que aparece en las páginas de categorías y tienda?

    Como esta tarjeta de producto que encontré: https://mega.nz/file/2scyWDjC#LTOuQu63JXwYLYVpdHr6XviGOkqZfQtF3vazii_nk_Y

    #1668539
    Elvin
    Staff
    Customer Support

    Hi Pedro,

    As this is already solved for the topic starter, can you open up a new topic? So you can use the private information text field incase it is needed.

    woocommerce_after_shop_loop_item_title is for the shop product items.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.