topherg
topherg

Reputation: 4293

Get Woocommerce Category Thumbnails

I have a custom template for a woocommerce category page to only display the categories. I have got the system to get a list of the child categories (by using get_term_children($id, 'product_cat') and get_term_by(...)), but it only returns objects containing all the required information, except the thumbnail data. Does anyone know how I can get the thumbnail for the term?

Upvotes: 18

Views: 58920

Answers (6)

SEO Helps
SEO Helps

Reputation: 15

Try this code to solve the issue 'get_woocommerce_term_meta is deprecated since version 3.6!'

  • Install plugin Snippets
  • add this code as a php code
  • save
  • active

then check the product category page to solve the issue.

add_action( 'woocommerce_archive_description', 
function() {
if( ! is_product_category() ) { return; }
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 sprintf( '<div style="background-image: 
url( \'%s\' );" /></div>', $image );
}
} );

Upvotes: 0

mohamad mehdi hajati
mohamad mehdi hajati

Reputation: 21

try this one

<?php 
    get_term_meta($term->term_id,'thumbnail_id',true);
?>

Upvotes: 0

Omid Ahmadyani
Omid Ahmadyani

Reputation: 1450

<?php
$thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
$image_url    = wp_get_attachment_url( $thumbnail_id ); // This variable is returing the product category thumbnail image URL.

Notice: get_woocommerce_term_meta is deprecated

Upvotes: 11

Eh Jewel
Eh Jewel

Reputation: 717

If the get_woocommerce_term_meta() function does not work for you then you can try the get_term_meta()function instead.

You can get the WooCommerce product category thumbnail with the following code-

<?php
$thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
$image_url = wp_get_attachment_url( $thumbnail_id ); // This variable is returing the product category thumbnail image URL.

Upvotes: 11

Derek
Derek

Reputation: 5037

Had a similar setup but when I used what you did I didnt actually get the thumbnail file I got the full image file so instead I used this: wp_get_attachment_thumb_url so that my output url would be "../my-images"/image-150x150.jpg" and actually got it to pull the thumbnail image, just incase anyone runs into a similar situation..

Upvotes: 3

topherg
topherg

Reputation: 4293

Sorted it, here's the code I used:

$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );

Upvotes: 32

Related Questions