【发布时间】:2020-04-30 20:13:50
【问题描述】:
我在 Wordpress 构建中运行高级自定义字段,并在图像附件中添加了一个 Select 字段类型。 字段名称是image_category,我有三个选择:top、middle、bottom。这些图像将在不同的迭代中围绕站点的多个部分调用。有些部分有多个图像,我想一起显示,但应该随机排序。在下面的示例中,我只调用了一张选择了类别bottom 的图像。但是,这似乎找不到图像,这可能是现场问题吗?我还没弄清楚如何使用$get_field() 函数调用这些图像,在我看来,这似乎比分类法更容易?非常感谢任何帮助!
<?php // Get Banner Advertisement Posts
$banneradverts = array(
'posts_per_page' => 1,
'post_type' => 'attachment',
'post_status' => 'any',
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'image_category',
'field' => 'slug',
'terms' => 'bottom',
),
),
);
$banners = new WP_Query( $banneradverts );
while ( $banners->have_posts() ) : $banners->the_post();?>
<section id="bottom">
<div class="bottom-image">
<?php echo wp_get_attachment_image( get_the_ID(), 'full'); ?>
</div>
</section>
<?php endwhile;
wp_reset_query(); ?>
【问题讨论】:
-
您自己创建了 post_type 'attachment' 吗?或者您是否尝试从 WordPress 检索特定附件?
-
只是试图获取在“image_category”字段名称中选择了“底部”的任何图像
-
好的。由于“image_category”是一个自定义字段,因此您需要执行“meta_query”而不是“tax_query”。您需要这方面的帮助吗?
标签: php wordpress advanced-custom-fields