php - 使用 ACF 调用图像选择字段类型
问题描述
我在 Wordpress 构建上运行高级自定义字段,并向图像附件添加了选择字段类型。字段名称是,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(); ?>
解决方案
<?php
$args = array(
'posts_per_page' => 1,
'post_type' => 'attachment',
'post_status' => 'any',
'orderby' => 'rand',
'meta_key' => 'image_category',
'meta_value' => 'bottom',
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<section id="bottom">
<div class="bottom-image">
<?php echo wp_get_attachment_image( get_the_ID(), 'full'); ?>
</div>
</section>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
推荐阅读
- python - 如何对 sphinx/docutils 指令进行单元测试
- php - 如何过滤 php 中的 the_content() 函数只返回一个特定的 div 类?
- python - 在 Python 中比较 csv 文件中的两行
- python-3.x - 当我运行的线程太多时我的游戏崩溃了,我不知道为什么
- php - PHP 致命错误:找不到类“SmartySecurity”
- android - 父母上的 wrap_content 和孩子上的 match_parent 之间的冲突
- java - 分层架构中的 Spring Boot RESTful API 验证
- google-apps-script - 是否可以使用 Google Apps 脚本解压缩 7zip 文件格式的文件?
- java - 年末 SimpleDateFormatter 的问题
- javascript - 如何在多个复选框过滤器中缩小搜索结果?