1

I know how to add an image and a gallery. I need to have one image from the galley show on the page at random each time the page is loaded.

The page should only show one image at a time.

Is there a plugin or short code to do this? I know how to make gallery's random, but they show all the images.

Answer:

$args = array( 
                'post_type' => 'attachment',
                'numberposts' => 1,
                'orderby' => 'rand',
                'post_status' => null,
                'post_parent' => get_the_ID(),
                'post_mime_type'  => 'image'
            ); 
            have_posts(); //must be in the loop
            the_post(); //set the ID

            $images = get_children( $args );            

            if ($images) {
            foreach ( $images as $attachment_id => $attachment ) {
                    echo wp_get_attachment_image( $attachment_id, 'full' );
                }
            }
            wp_reset_query();

3 Answers 3

3

You should use the 'orderby' => 'rand' parameter for the get_children() attachments function.

For instance:

$images = get_children( array(
    'orderby'        => 'rand',       // this is random param
    'post_type'      => 'attachment',
    'post_mime_type' => 'image',
    'post_parent'    => get_the_ID(),
);
2
  • this works but adds image from the library and not the gallery. I need it to be from the gallery so each set differs from page to page.
    – Kevin
    Commented Mar 28, 2011 at 0:19
  • 1
    your post and this one fix the issue stackoverflow.com/questions/2640520/…
    – Kevin
    Commented Mar 28, 2011 at 0:46
1

You can also pull the IDs from ALL galleries on a page using get_post_galleries() and you don't need an extra loop.

// pull all the images from all galleries as unique IDs
$images = array_unique( explode( ",", implode( ",", wp_list_pluck( get_post_galleries( get_the_ID(), false ), 'ids' ) ) ) );

// randomize the order
shuffle( $images );

// pull the first id
list ( $id ) = $images;

// convert to image
echo wp_get_attachment_image( $id, 'full' );

Reference

0

This code answers the question. In a page or post containing a gallery, it will take one random image from the gallery and display only that image. It goes inside the wordpress loop ( code here actually includes the loop ).

<?php //start the loop ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

            <?php // get the post's gallery ?>
            <?php if ( get_post_gallery() ) : $gallery = get_post_gallery( get_the_ID(), false ); ?>

                <?php //get gallery picture ids string seperates the ids and puts them in an array. ?>
                <?php $pic_ids = explode(",", $gallery['ids']);?>

                <?php // set a random int < to the size of the array containing ids.?> 
                <?php $i=rand(0, count($pic_ids)-1);?>

                    <?php //get image corresponding to the random id?>
                    <?php echo wp_get_attachment_image( $pic_ids[$i],'full', false, '' ); ?>
                <?php endif; ?>
        <?php endwhile; else : ?>

            <p><?php _e( 'Sorry, no page found.' ); ?></p>

        <?php endif; ?>
3
  • Please add an explanation to what your code does, how it works and how it should be used Commented Mar 5, 2016 at 10:55
  • That info should be inside your answer, not a comment. Please file an edit and add the relevant info in your answer Commented Mar 7, 2016 at 12:50
  • There you go. Would you vote up this is actually a valid and useful answer. Commented Mar 7, 2016 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.