4

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); ?>" />
Lucien Dubois
  • 1,590
  • 5
  • 28
  • 53

1 Answers1

3

If you look at the result of the queried_object dump, you should see a "parent" key.

["parent"]=> int(0)

A 0 means there is no parent, otherwise the number will be the ID of the parent taxonomy.

So what you want to do is something like this.

<?php 
$queried_object = get_queried_object(); 
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;

if($queried_object->parent) {
    $term_id = $queried_object->parent;
}
?>

<img src="<?php the_field('grosse_image_carre', $term_id); ?>" />

So we are first getting the queried_object, and setting the $term_id variable to be it's ID. Then we check to see if the parent key is set (0 means false) and if there is one, then we set $term_id as the parent ID instead of the queried object's ID. Then you just change the ID parameter on the_field to be $term_id and bob's your uncle :)

EDIT:

Based on the updated information that Lucien wants the top level category if we are more than 1 parent deep:

if($queried_object->parent) {
    $parent = $queried_object->parent;
    while($parent) {
        $cat = get_category($parent);
        $parent = $cat->category_parent;
        $term_id = $cat->cat_ID;
    }
}

Now $term_id is set to the top-level category ID.

EDIT 2:

Though the code above will work for regular WordPress categories, I forgot we are wanting to work with a custom taxonomy (in this case WooCommerce product_cat taxonomy.)

So with that in mind, here is the code. I have tested this on a WooCommerce installation, with a 4 deep category and it returned the very top category.

$queried_object = get_queried_object(); 
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id; 

if($queried_object->parent) {
    $parent = $queried_object->parent;
    while($parent) {
        $cat = get_term($parent, $taxonomy);
        $parent = $cat->parent;
        $term_id = $cat->term_id;
    }
}

As before, you will use $term_id in the ACF function to retrieve the image.

<img src="<?php the_field('grosse_image_carre', $queried_object); ?>" />

This code should also work out-of-the-box with any new custom taxonomies due to the fact that we are not hard-coding the name of the taxonomy. Everything being retrieved is automatic.

Josh Riser
  • 191
  • 6
  • How would I do if I want to go back until it gets the top-level parent id? Would that be of any help? http://alex.leonard.ie/2011/04/20/wordpress-get-id-of-top-level-parent-category/ – Lucien Dubois Jul 21 '15 at 19:02
  • Ah sorry. Forgot we were working with a custom taxonomy. I have updated again with the new code which I have tested on a WooCommerce installation and it is working. Also if you want to echo the variables, make sure you do it outside of the **while** loop (preferably after it). Also you will have to **print_r** the **$cat** variable, as this will be an object and not a string. – Josh Riser Jul 22 '15 at 18:40
  • Thank you! It works with your EDIT2 and 'product_cat_'.$term_id as the second parameter for the_field function. – Lucien Dubois Jul 22 '15 at 19:58