Support

Account

Home Forums Add-ons Gallery Field Gallery – Random and Limiter

Solved

Gallery – Random and Limiter

  • Hi, I am currently using this code with the Gallery field type:

    <?php $images = get_field('photo_gallery','17'); if( $images ): ?>
        <ul>
            <?php foreach( $images as $image ): ?>
                <li>
                    <a href="<?php echo $image['url']; ?>" rel="lightbox">
                         <img src="<?php echo $image['sizes']['photo-gallery']; ?>" alt="<?php echo $image['alt']; ?>" />
                    </a>
                </li>
            <?php endforeach; ?>
            <div id="clearfloat"></div>
        </ul>
    <?php endif; ?>

    I need to adjust the coding to make it limit the amount of images and randomize the images. Can you help? Thanks

  • New code mixed into yours and commented

    
    <?php 
        $images = get_field('photo_gallery','17'); 
        if( $images ):
            shuffle($images); // randomizes the image array
            $max = 10; // set the max here;
            $count = 0; // current count
    ?>
        <ul>
            <?php 
                foreach( $images as $image ):
                    $count++; // increment count
                    // if count is greater than limt, stop showing images
                    if ($count > $max) {
                        break;
                    }
             ?>
                <li>
                    <a href="<?php echo $image['url']; ?>" rel="lightbox">
                         <img src="<?php echo $image['sizes']['photo-gallery']; ?>" alt="<?php echo $image['alt']; ?>" />
                    </a>
                </li>
            <?php endforeach; ?>
            <div id="clearfloat"></div>
        </ul>
    <?php endif; ?>
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Gallery – Random and Limiter’ is closed to new replies.