0

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(
    'name'=>'header-images',
    'numberposts' => 4,  
    'orderby' => 'rand'
);
query_posts($args);
if (have_posts()) : 
    while (have_posts()) : the_post();?>
        <div id="headerImages">
            <?php the_content(); ?>
        </div>
    <?php endwhile;
endif;
wp_reset_query();

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

Try this:

$args = array(
    'name'=>'header-images',
    '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:

http://codex.wordpress.org/Function_Reference/query_posts

Edit:

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){
        $output=array();
        $my_query = new WP_Query(array('name'=>$slug,'post_type'=>$type));
        while ($my_query->have_posts()) : $my_query->the_post(); 
            $content=get_the_content();
            preg_match('/ids=\"([0-9,]+)\"/i', $content, $matches);
            if(isset($matches[1])){
                $output = explode(",",$matches[1]); // let's take the last set of ids
            }           
        endwhile;
        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:
$ids=get_gallery_ids_wpse_87978($slug,$type);

// get n random keys from the $ids array:
$random_ids=array_rand($ids,$n);

// 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>';
5
  • I tried that and unfortunately the result didn't change – Devin Crossman Feb 22 '13 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 '13 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" – Devin Crossman Feb 22 '13 at 19:31
  • I updated the answer, hopefully it's in the right direction ;-) – birgire Feb 23 '13 at 3:55
  • perfect! the function in your updated answer was exactly what I needed. Thanks! – Devin Crossman Feb 24 '13 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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