I use Woocommerce and Advanced Custom Fields to manage products. I would like to show a parent taxonomy image on sub-taxonomy pages.
Actually, I use this snippet to get the image on the taxonomy parent page (it works):
<?php
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
?>
<img src="<?php the_field('grosse_image_carre', $queried_object); ?>" />
For reference purpose, here is the var_dump of each variable:
queried_object
object(stdClass)#5089 (10) { ["term_id"]=> int(341) ["name"]=> string(12) "France Laure" ["slug"]=> string(12) "france-laure" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(341) ["taxonomy"]=> string(11) "product_cat" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(185) ["filter"]=> string(3) "raw" }
taxonomy
string(11) "product_cat"
term_id
int(341)
My question: How to get the parent category image when I am on a sub-category page?
Ref: Get values from a taxonomy term
EDIT
I tried the solution proposed and I used this complete code:
<?php
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
if($queried_object->parent) {
$parent = $queried_object->parent;
echo 'parent: '.$parent.'<br />';
while($parent) {
$cat = get_category($parent);
echo 'Cat: '.$cat.'<br />';
$parent = $cat->category_parent; // Line 30
echo 'parent: '.$parent.'<br />';
$term_id = $cat->cat_ID; // Line 32
echo 'term_id: '.$term_id.'<br />';
}
}
?>
But I got
parent: 359
Cat:
Notice: Trying to get property of non-object in ...woocommerce\subcat-archive-product.php on line 30
parent:
Notice: Trying to get property of non-object in E...woocommerce\subcat-archive-product.php on line 32
term_id:
EDIT 2
I followed Josh Edit 2 and it finally works! I use
<img src="<?php the_field('grosse_image_carre', 'product_cat_'.$term_id); ?>" />