I'm not sure if I'm doing this the right way.. I want to display 4 random images as part of the header of a page. I created a post called Header Images and inside it is a gallery with a bunch of images. Then inside my header.php I have

$args = array(
    'numberposts' => 4,  
    'orderby' => 'rand'
if (have_posts()) : 
    while (have_posts()) : the_post();?>
        <div id="headerImages">
            <?php the_content(); ?>
    <?php endwhile;

which does randomize the order and display the images but it doesn't limit them to 4 like I want. Can anyone explain to me what I need to do? Thanks!

1 Answer 1


Try this:

$args = array(
    'posts_per_page' => 4,  
    'orderby' => 'rand'

i.e. use posts_per_page instead of numberposts if you want to use query_posts() according to the the Codex page:



Here is one idea - fetch the gallery shortcode ids from a page content with a given slug:

* Get an array of the gallery shortcode ids from a page content with a given slug 
* @param string $slug Post slug.  
* @param string $type Post type.  
* @return array Array of the exploded ids parameter.
function get_gallery_ids_wpse_87978($slug,$type){
        $my_query = new WP_Query(array('name'=>$slug,'post_type'=>$type));
        while ($my_query->have_posts()) : $my_query->the_post(); 
            preg_match('/ids=\"([0-9,]+)\"/i', $content, $matches);
                $output = explode(",",$matches[1]); // let's take the last set of ids
        return $output;

Usage example:

Let's say we have a page with the slug my-gallery-demo and in the content there is a shortcode like this one:

[gallery ids="1376,1375,341,213,211,210,209,208,206,205"]

To display 4 random thumb images from this shortcode we do the following:

// initial values:
$slug='my-gallery-demo'; // EDIT post/page slug that contains the gallery shortcode 
$type='page'; // EDIT post type (post,page,...) 
$size='thumb'; // EDIT image size (thumb,large,full,...)
$n=4; // EDIT number of random images to show

// fetch all ids from the gallery shortcode:

// get n random keys from the $ids array:

// display a list of n random images:
echo '<ul>';
foreach($random_ids as $random_id){
      echo '<li>';
      echo wp_get_attachment_image( $ids[$random_id], $size );
      echo '</li>';
echo '</ul>';
  • I tried that and unfortunately the result didn't change Feb 22, 2013 at 19:22
  • ok so you have a page and want to display 4 random image attachments that belong to that page?
    – birgire
    Feb 22, 2013 at 19:25
  • It's part of the header for all the pages. I'm not sure that the attachments belong to that page? the images are inside a gallery that is inside a post called "Header Images" Feb 22, 2013 at 19:31
  • I updated the answer, hopefully it's in the right direction ;-)
    – birgire
    Feb 23, 2013 at 3:55
  • perfect! the function in your updated answer was exactly what I needed. Thanks! Feb 24, 2013 at 9:55

Your Answer

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

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