1

I am using advanced custom field(image upload) and the rule is to show that field if taxonomy term is product category.

The image upload field is showing properly on product category page in admin and the file is being uploaded properly.

Now I want to fetch the value using get field. My code is like

$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$image = get_field('product_cat_banner',$current_term->taxonomy.'-'.$current_term->term_id);

And it returns nothing.As stated here, I have done exactly the same but can't understand why it's not getting the value.

Any help is highly appreciated. Thanks in advance.

1 Answer 1

6

Try this:

$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$image = get_field('product_cat_banner',$current_term->taxonomy.'_'.$current_term->term_id);

In the second line, change your hyphen between $current_term->taxonomy and $current_term->term_id to an underscore. As-per the example you linked, that is the way to output the taxonomy term's ID. So if your taxonomy was "Products" and your Product Term ID was 12, you would output that term ID as "products_12".

So just swap the hyphen for an underscore, and the rest of the code looks right.

2
  • Now I understand how stupid I am. Wasted 2 hrs.. Thanks a lot Joe. You saved my life.
    – Raj
    Commented Jun 10, 2015 at 12:43
  • 1
    Sometimes it's the little things like punctuation marks that make a huge difference. I've certainly wasted plenty of time on stray or missing commas.
    – Joe
    Commented Jun 10, 2015 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.