0
votes

In Advanced Custom Fields (ACF) I created an image field and added that to the categories of a custom post type so every category can have his own image. Then I created a loop that displays the categories of that custom post type. This works but I can't get the image that I uploaded in the ACF field to display.

The code for displaying the categories of the custom post type:

<?php $taxonomy = 'customposttype'; $terms = get_terms($taxonomy); if ( $terms && !is_wp_error( $terms ) ) : ?>
<?php foreach ( $terms as $term ) { ?>      

    <?php echo $term->name; ?>
    // display acf image

<?php } ?>  
<?php endif;?>

I tried to work with 'array_combine' but I couldn't get that to work.

1
so you are not getting the image of category ? Please give the slug of taxonomy. - A Shah
@AkshayShah indeed! Slug is 'behandeling'. ACF field name is: 'category_afbeelding' - Erwin van Ekeren
try with my code now - A Shah
@AkshayShah it seems to break the code, i don't think get_image exists in ACF? And nowhere its referring to 'category_afbeelding'... - Erwin van Ekeren
Please check my answer - A Shah

1 Answers

1
votes

Try this code in the loop

$term_image = get_term_meta( $term->term_id , 'category_afbeelding', true);

$image_attributes = wp_get_attachment_image_src( $term_image,’full’);
 if ( $image_attributes ) : 
  <img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>" />
endif;