Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Okay, so I found this code online that allows you to add the option of how many thumbnails you want to show in the gallery shortcode.

The code below is what they say to add to the functions.php file

      function get_random_gallery_images(){  
global $wpdb,$post;  
    $ids = "";  
    $counter = 0;  
    $number_of_posts = 4;  
    $args = array(  
    'post_type' => 'attachment',  
    'numberposts' => 4,  
    'post_status' => null,  
    'orderby' => 'rand',  
    'post_parent' => $post->ID  
    );  
    $attachments = get_posts($args);  
    if ($attachments) {  
        foreach ($attachments as $attachment) {  

            if ($counter != 0) {  
                $ids .= ','.$attachment->ID;  
            }  
            else {  
                $ids .= $attachment->ID;  
            }  
            $counter++;  
        }  
    }  
   return $ids;  }  

Then they say add this code below to the loop

       $attachment_ids = get_random_gallery_images();  
       echo do_shortcode('[ gallery columns="4" include="'.$attachment_ids.'" link="file" ]');

But my problem is, they didn't say where in the loop to add the code. Does anyone know where I can add this code? Also if you have a better solution to achieve what I am asking, please share :)

share|improve this question

1 Answer 1

up vote 0 down vote accepted

The function you've supplied actually doesn't look that bad. It's almost the same way I'd handle the problem, however if you only ever want to modify the include part and always get the attachment_ids associated with the post, here is what I would do.

I would actually create a new shortcode which in turn calls the gallery shortcode.

Inside of your functions.php file:

    function get_random_gallery_images(){  
global $wpdb,$post;  
    $ids = "";  
    $counter = 0;  
    $number_of_posts = 4;  
    $args = array(  
    'post_type' => 'attachment',  
    'numberposts' => 4,  
    'post_status' => null,  
    'orderby' => 'rand',  
    'post_parent' => $post->ID  
    );  
    $attachments = get_posts($args);  
    if ($attachments) {  
        foreach ($attachments as $attachment) {  

            if ($counter != 0) {  
                $ids .= ','.$attachment->ID;  
            }  
            else {  
                $ids .= $attachment->ID;  
            }  
            $counter++;  
        }  
    }  
   return $ids;  }

function multi_gallery_shortcode()
{
    $attachment_ids = get_random_gallery_images();
    return do_shortcode('[gallery columns="4" include="'.$attachment_ids.'" link="file"]');
}


add_shortcode('multigallery', 'multi_gallery_shortcode'); 

And then inside of your post add this whenever you want to display the gallery. You add this code in via the editor, not any template files:

[multigallery]

The code supplied is highly conceptual in that I haven't tested it, but it should work and do what you want it to. If not, let me know and we'll sort it out.

Another issue I spotted and I could be wrong about this is the in the line of code you supplied, this line to be specific: do_shortcode('[ gallery columns="4" include="'.$attachment_ids.'" link="file" ]'); - had a space after the first opening bracket and last closing bracket which will mean it won't render as a proper shortcode. In my code I fixed this up.

share|improve this answer
    
THANK YOU that works perfectly agggghhhhhhh i'm excited lol –  Terrell Anderson Mar 2 '12 at 11:34
    
Haha, no worries Terrell. Glad I could help, don't forget to mark the answer as accepted by clicking the tick button beside the answer. –  DigitalSea Mar 2 '12 at 11:37
    
Also should state that if they want the thumnail to go to the attachment page just erase link="file" –  Terrell Anderson Mar 2 '12 at 12:15

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.