最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - ACF - Custom image for Product Tags not displaying

programmeradmin3浏览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: /

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?

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/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?

Share Improve this question asked Jan 22, 2020 at 13:02 Pbalazs89Pbalazs89 1151 silver badge12 bronze badges 3
  • 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 Commented Jan 22, 2020 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 Commented Jan 22, 2020 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 Commented Jan 22, 2020 at 13:15
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论