0

I'm fairly new to PHP so sorry if its something elementary.

I'm trying to modify my shop template, so that I can list certain icons for each tag. I installed ACF pro and created a custom_image for product_tag

it is called: tag_taxonomy_image

I managed to create a loop which checks for a certain tag (right now it's 1EUR) and echos if it is found.

I'm trying to output the image associated with this product_tag, but I can't. I'm trying to follow this document: https://www.advancedcustomfields.com/resources/image/

I'm modifying the content-product-php, and here's my code currently:

<?php
/* Get the product tag */
$terms = get_the_terms( $post->ID, 'product_tag' );


$product_tag_check = array();
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    foreach ( $terms as $term ) {
        $product_tag_check[] = $term->name;
    }
}

/* Check if it is existing in the array to output some value */




if(in_array('1EUR',$product_tag_check)) {
    $term = get_term_by( 'id', $child, $taxonomy_name );
    $image = get_field('tag_taxonomy_image', $term); //'tag_taxonomy_image' is our field name
    $size = 'thumbnail'; // (thumbnail, medium, large, full or custom size)

    echo '1EUR tag exists';
    echo $image['url'];
    echo wp_get_attachment_image( $image, $size );


?>


<?php   } else { 
   echo '1EUR tag does not exist';
   }


   ?>

Any recommendations on what I'm missing?

  • That looks sensible at first glance. Can you be more specific about what's going wrong: you do see '1EUR tag exists' but the image is broken? Can you check that $term and $image are set correctly? – Rup Jan 22 at 13:07
  • Hi @Rup, Thanks for having a glance at it. I created a short vid(30sec to explain the issue) youtu.be/wymxwv5C6TE . So I'm trying to output an image for each tag on the page. The 1EUR tag exists appears, but I can't seem to grab the image ACF field tag_taxonomy_image. – Pbalazs89 Jan 22 at 13:10
  • Yes, I understand broadly what you're trying to do - I meant what specifically happens with your code. OK, so it looks like you're getting nothing from echo $image['url']; or echo wp_get_attachment_image( $image, $size );. I'd start by dumping $image and $term, e.g. print_r( $term ); to check they look correct. But I haven't used ACF for a while - it might be best asking them for help if you get stuck. – Rup Jan 22 at 13:15
0

to grab the ACF data you will need to use $tag_tax_image = get_field('tag_taxonomy_image', $post->ID);

From there I recommend doing a var_dump($tag_tax_image); to figure out how to access the data you need.

  • It looks like it's a custom field on the term not the post, so you need to pass a WP_Term object I think. – Rup Jan 22 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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