Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce Display Category Image - get_woocommerce_term_meta deprecated

I'm in need of some help.

I want to display the category image on the current category page, and I have googled this, and each answer I find uses the same code.

They all use get_woocommerce_term_meta to retrieve the ID of the thumbnail used so that you can then use wp_get_attachment_url to get the image address.

All sounds great, but whenever I try this code it returns nothing, and I think that it is because get_woocommerce_term_meta is deprecated.

Does anyone know of a way round this, so I can get the image address when I have the category ID?

This is the code that I have in place:

global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta($cat->term_id, 'thumbnail_id', true);
echo $cat->term_id."<br />";
echo $thumbnail_id;
$image_src = wp_get_attachment_url($thumbnail_id);

$cat->term_id returns the correct ID of the category, but $thumbnail_id returns 0.

The code is in header.php.

like image 666
Bev Love Avatar asked Oct 30 '22 13:10

Bev Love


1 Answers

get_woocommerce_term_meta() may be deprecated however it hasn't yet been removed. The issue is elsewhere in your setup.

With that said we can resolve the deprecation issue quite easily. Simply replace usages of get_woocommerce_term_meta() with the new native WordPress function get_term_meta().

get_woocommerce_term_meta() will simply pass its arguments on to the new function anyway so we can be sure the problem isn't with the deprecated function.

Likely causes of the issue:

  • Key used to save the image isn't the same as the key being used to retrieve
  • Not passing in correct term ID
  • No image set
like image 159
Nathan Dawson Avatar answered Jan 02 '23 20:01

Nathan Dawson