Home Forums Add-ons Gallery Field Limit and randomize the display of images in a Gallery field on template


Limit and randomize the display of images in a Gallery field on template

  • Hi there Eliot,

    I’m in a circumstance where I need to both limit and randomize the images of a gallery field shown on a template. This area of the template is intended to be a preview of the gallery that links (through a “see the whole gallery” button) to a page where the full gallery is shown.

    The gallery is not directly attached to the custom page fields, but through a relationship field called page_sidebar_gallery (so I can select the same gallery on multiple pages). My code looks like this:

    $ids = get_field('page_sidebar_gallery', false, false);
    if ( $ids ) {
    $gallery_query = new WP_Query(array(
       'post_type'      	=> 'galleries',
       'post__in'		=> $ids,
       'post_status'		=> 'any',
    while ( $gallery_query->have_posts() ) {
    $gallery_title = get_the_title();
    $gallery_permalink = get_the_permalink();
    $gallery_images = get_field('photogalleries_gallery');
    foreach ( $gallery_images as $image ): ?>
    <div class="thumb-sidebar">												<a href="<?php echo $image['url']; ?>" rel="lightbox" class="thumbnail thumbnail-gallery-cell" style="background:url(<?php echo $image['sizes']['medium']; ?>); background-position: center center; background-repeat: no-repeat; background-size: cover;" />												</a><!-- ends thumbnail -->							</div><!-- ends thumbnail col -->
    <?php endforeach; 
    echo '<a class="btn btn-info" href="' . $gallery_permalink . '">View the full gallery &raquo;</a>';

    However, I’m not sure where I can set output parameters in the foreach loop. While I can use wp_query for the outside loops, I don’t seem to have similar parameter options in the ‘foreach ( $gallery_images as $image )’.

    Thanks for your help pointing me in the right direction!

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.