I have added an acf image gallery for archive terms and I would like to take those images and insert one image from the gallery every 3 posts in the term archive. This is what I have so far but I am not yet getting them to distribute sequentially every 3 posts.
1-2-3 posts
Image 1
4-5-6 posts
Image 2
7-8-9 posts
Image 3
// get the current taxonomy term
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$images = get_field('term_gallery', $taxonomy .'_' . $term_id); //image ids
$size = 'full'; // (thumbnail, medium, large, full or custom size)
$counter = 1;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content', get_post_type() );
?>
<?php /*//////////////////INSERT IMAGE///////////*/?>
<?php if($counter % 3 == 0): ?>
<?php if( $images ): ?>
<?php foreach( $images as $image_id ): ?>
<div class="image-<?php echo $counter;?>">
<?php echo wp_get_attachment_image( $image_id, $size ); ?>
</div>
<?php endforeach; ?>
<?php endif;?>
<?php endif;?>
<?php /*/////////////////INSERT IMAGE///////////*/?>
<?php
$counter++;
endwhile;
endif;
With the above the entire gallery is shown every 3 posts. As in
1-2-3 posts
Image 1 + Image 2 + Image 3
4-5-6 posts
Image 1 + Image 2 + Image 3
UPDATE don't know how i got this but it works partly, it just doesn't work with pagination after first page? No images at all show on the next page when I expected to get the next 3 images of the gallery.
// get the current taxonomy term
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$images = get_field('term_gallery', $taxonomy .'_' . $term_id);
$size = 'full'; // (thumbnail, medium, large, full or custom size)
$images_len = count($images); // max
$counter = 1;
$img_ct = 0;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content', get_post_type() );
?>
<?php /*//////////////////INSERT IMAGE///////////*/?>
<?php if( $images ): ?>
<?php if($counter % 3 == 0): ?>
<?php if($img_ct == $images_len ) $img_ct = 0;?>
<div class="image-<?php echo $img_ct;?>">
<?php echo wp_get_attachment_image( $images[$img_ct], $size ); ?>
</div>
<?php $img_ct++;?>
<?php endif;?>
<?php endif;?>
<?php /*//////////////////INSERT IMAGE///////////*/?>
<?php
$counter++;
endwhile;
endif;
$counter % 6 == 0
to$counter % 3 == 0
, and how does it not work with the pagination - e.g. what's not showing up that should show up?$images
to check if it actually does have more than 3 items? But your code now seems fine, except you'd want to slice the array so that the images are distributed as expected across all pages. E.g.$images = get_field( ... ); $images = array_slice( $images, 0, 3 );