WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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();
share|improve this question
up vote 3 down vote accepted

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(),
);
share|improve this answer
    
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 D. Mar 28 '11 at 0:19
1  
your post and this one fix the issue stackoverflow.com/questions/2640520/… – Kevin D. Mar 28 '11 at 0:46

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

share|improve this answer

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; ?>
share|improve this answer
    
Please add an explanation to what your code does, how it works and how it should be used – Pieter Goosen Mar 5 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 – Pieter Goosen Mar 7 at 12:50
    
There you go. Would you vote up this is actually a valid and useful answer. – Alexandre Babeanu Mar 7 at 13:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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