Gallery Shortcode Function Help

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


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.