Site logo

[Resolved] How to display the tags of each product on the shop page

Home Forums Support [Resolved] How to display the tags of each product on the shop page

Home Forums Support How to display the tags of each product on the shop page

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2130926
    Daniel

    Hi!

    On the shop page I would like to show below the price of each product the tags of the product preceded by the text: “Tag:” if it is one tag or “Tags:” if there are several tags. I also need the terms to be text, not links.

    Can you tell me how to achieve this?

    Thanks!
    Daniel

    #2131005
    Leo
    Staff
    Customer Support

    Hi Daniel,

    This isn’t something the theme can control as that’s handled by WooCommerce templates.

    I would recommend checking with WooCommerce’ support team for recommendations.

    I’d imagine that you’d need to write a function to display the tags and add it using a hook:
    https://www.businessbloomer.com/woocommerce-visual-hook-guide-archiveshopcat-page/

    #2131588
    Daniel

    Here is the solution in case anyone else is looking for the same thing. It includes the link for the tags, which I’m sure is more useful for most people.

    add_action('woocommerce_after_shop_loop_item', 'show_tags', 5);
    function show_tags()
    {
        global $product;
        // get the product_tags of the current product
        $current_tags = get_the_terms(get_the_ID() , 'product_tag');
        // only start if we have some tags
        if ($current_tags && !is_wp_error($current_tags))
        {
            echo '<p class="product_tags">';
            //append Tag or Tags
            if (count($current_tags) == 1)
            {
                echo '<span class="product-tags">Tag: </span>';
            }
            else
            {
                echo '<span class="product-tags">Tags: </span>';
            }
            // for each tag we create an item
            foreach ($current_tags as $tag)
            {
                $tag_title = $tag->name; // tag name
                $tag_link = get_term_link($tag); // tag archive link
                $separator = ' | ';
                $tagstrings[] = '<a href="' . $tag_link . '">' . $tag_title . '</a>';
            }
            echo implode($separator, $tagstrings) . '</p>';
        }
    }
    #2132298
    Leo
    Staff
    Customer Support

    Thanks for sharing again!

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