How to add a random image to a post from gallery and only show one?

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.

Read More

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();

Related posts

Leave a Reply

3 comments

  1. 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. 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

  3. 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; ?>