I am running Advanced Custom Fields on a Wordpress build and I added a Select Field Type to image attachments. The Field Name is image_category
and I have three selections: top
, middle
, bottom
. These images will be called around multiple parts of the site in different iterations. Some sections have multiple images that I want to show together but should be ordered randomly. In the bottom example, I am only calling one image that has the category bottom
selected. This doesn't seem to find the image however, could this be a field issue? I haven't figured out how to call these images using the $get_field()
function which in my head, seems easier than taxonomies? Any help is much appreciated!
<?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; ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.