【问题标题】:Calling an image using ACF Select Field Type使用 ACF 调用图像选择字段类型
【发布时间】:2020-04-30 20:13:50
【问题描述】:

我在 Wordpress 构建中运行高级自定义字段,并在图像附件中添加了一个 Select 字段类型。 字段名称image_category,我有三个选择:topmiddlebottom。这些图像将在不同的迭代中围绕站点的多个部分调用。有些部分有多个图像,我想一起显示,但应该随机排序。在下面的示例中,我只调用了一张选择了类别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


【解决方案1】:
<?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; ?>

【讨论】:

    猜你喜欢
    • 2015-03-12
    • 1970-01-01
    • 2022-06-23
    • 2022-10-02
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 2015-12-04
    • 1970-01-01
    相关资源
    最近更新 更多