使用ACF从自定义分类中提取图像 [英] Pulling in image from custom taxonomy with ACF

查看:124
本文介绍了使用ACF从自定义分类中提取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的。我创建了一个称为国家的自定义分类法,在该自定义分类法中,我具有不同国家的类别,例如美国,法国,德国,英国等。我创建了一个ACF(高级自定义字段)字段组,该字段组允许我向每个国家/地区添加图片,我将其称为标志。因此,当我发布帖子并根据自定义分类法检查国家/地区时,我需要拉出与该类别关联的图像(标志)并将其显示在我的页面上。

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>

它带回 NULL而不是我的图像ID。

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?

推荐答案

有您定义了$ queried_object?

have you defined $queried_object?

编辑:
试试这个

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>

这篇关于使用ACF从自定义分类中提取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆