Pulling in image from custom taxonomy with ACF

2019-08-17 05:06发布

So here is what i'm trying to do. I have created a custom taxonomy called "country" and within that custom taxonomy I have categories of different countries like, USA, France, Germany, England and so on. I created a ACF(Advance custom field) Field Group that lets me add a image to each country, I have this called "flag". So when I make a post and I check the country under the custom taxonomy I need to pull that image (flag) associated with that category and display it on my page.

I have seen lots of post on what im looking to do but I cant seem to get any to work. Here is the code im TRYING to use.

<?php
$attachment_id = get_field( 'flag', 'country_' . $queried_object->term_id );
$thumb = wp_get_attachment_image_src( $attachment_id );

var_dump($attachment_id);
?>
<p><img src="<?php echo $thumb[0]; ?>" /></p>

Its bring back a "NULL" and not my image id.

Any ideas on what i'm doing wrong or any advice you and lend me?

1条回答
狗以群分
2楼-- · 2019-08-17 05:45

have you defined $queried_object?

EDIT: TRY THIS OUT

<?php
$term =$wp_query->queried_object;
$attachment_id = get_field( 'flag', 'country_' . $term->term_id );
$thumb = wp_get_attachment_image_src( $attachment_id );

var_dump($attachment_id);
?>
<p><img src="<?php echo $thumb[0]; ?>" /></p>
查看更多
登录 后发表回答