I am using a child theme on genesis. I have set up a custom post type and a custom taxonomy, and I'm using Advanced Custom Fields to put a gallery on each page of the CPT. On the taxonomy archive pages, I would like to show a single image from each gallery as a thumbnail, linking to the post. I have successfully set this up using a new template for the custom taxonomy, but if I set 'numberposts' => -1 to show all the posts, I get eight repeats of each post/thumbnail. If I set it to 'numberposts' => 1, I get 3 repeats of each post/thumbnail. How do I show just a single thumbnail from each post?
I don't know for sure that this is a wordpress problem, but I think it must be something to do with how I am setting up the custom loop?
<?php
//* Template Name: Press Release; Archive
remove_action ('genesis_loop', 'genesis_do_loop'); // Remove the standard loop
add_action ('genesis_after_loop', 'collection_categories'); // add custom loop
function collection_categories(){
$terms = get_terms('collection_categories');
foreach($terms as $term) {
$posts = get_posts(array(
'post_type' => 'collections',
'tax_query' => array(
array(
'taxonomy' => 'collection_categories',
'field' => 'slug',
'terms' => $term->slug
)
),
'numberposts' => 1
));
foreach($posts as $post) {
//* One image from gallery on archive pages
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$images = get_field('gallery');
$image_1 = $images[0];
$link = get_the_permalink();
?>
<a href="<?php echo $link ?>"><img src="<?php echo $image_1['sizes'] ['thumbnail']; ?>" alt="<?php echo $image_1['alt']; ?>" /></a>
<?php
endwhile;
endif;
}
}
}
genesis();
while
loop inside anotherforeach
loop?