首页 > 解决方案 > 使用 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(); ?>

标签: phpwordpressadvanced-custom-fields

解决方案


<?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; ?>

推荐阅读