• Hello,

    I want to display my category image on the single product page. All of my products are only assigned to one specific category, no products have multiple categories.

    This code lets me display the category image for the current category on the category page, but I want to do exactly the same thing on the single product page.

    global $wp_query;
    $cat = $wp_query->get_queried_object();
    $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
    $image = wp_get_attachment_url( $thumbnail_id );
    if ( $image ) {
      echo '<img class="absolute category-image" src="' . $image . '" alt="" />';
    }

    How can I modify this to work on the single product page since, as stated above, none of my products have multiple categories?

    http://wordpress.org/plugins/woocommerce/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Filespit

    (@filespit)

    I did some tweaking and finally solved it. Here’s my code;

    $terms = get_the_terms( $post->ID, 'product_cat' );
    foreach ( $terms as $term ){
      $category_name = $term->name;
      $category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
      $image = wp_get_attachment_url($category_thumbnail);
      echo '<img class="absolute category-image" src="'.$image.'">';
    }

    Fantastic, you’re a lifesaver. I was looking around for this code for ages.

    Hi everybody and thank you for the code!
    I just have a little problem, maybe you can help me: what I’m trying to do is put the images of the category under the product image but chaging the image size of $image and display them inline (multiples category).
    I tried with <img width=”20″ height=”20″ src=”‘.$image.'”> and also with a custom class I created (height:20px; !important; width:20px; !important) but it does not work and I cannot change the original category_thumbnail size because I need it in the shop front page. Do you have a suggestion to change the size of thoose images without changing the global size of the thumbnail?
    This is an example: http://puu.sh/6DwaS/c999ab4704.jpg
    Thank you very much for every suggestion.

    I found the solution trying different options.
    For the images:
    max-width: 50px; height: auto;
    For the container:
    display: inline-flex;

    The result is wonderful!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Display category image on single product page’ is closed to new replies.