1

i am using the [multigallery] function and I was wondering if there is a way to use the function to call attachments from another post.

For example if i was writing a post about taylor swift and I wanted to include attachments from another taylor swift post using the multigallery shortcode how would I do so.

Like by adding id= to the shortcode [multigallery id=???] where the id would be the post number and it will grab attachments from that post.

here is the code that i am currently using in my function.php file

function multi_gallery_shortcode(){
global $post;
$before     = sprintf('<div class="gallery-before">%s</div>',$post->post_title); 
$gallery_sc = sprintf('<div id="gallery-1">[gallery columns="6" order="ASC" orderby="menu_order" include="%s" link="gallery"]</div>',get_random_gallery_images($post->ID));
$after      = sprintf('<div class="gallery-after"><span class="galNum">%d</span> Photos</div><div style="clear:both;"></div>', get_total_attachments($post->ID));
return $before . do_shortcode($gallery_sc) . $after;
}

function get_random_gallery_images($post_id){ 
global $wpdb; 
$ids = ""; 
$args = array( 
    'post_type'      => 'attachment', 
    'post_mime_type' => 'image', 
    'posts_per_page' => 6, 
    'post_status'    => 'any', 
    'orderby'        => 'rand', 
    'post_parent'    => $post_id, 
); 
$attachments = get_posts($args); 
if ($attachments) { 
    $tmp=array();
    foreach ($attachments as $attachment) {
            $tmp[] = $attachment->ID; 
    }  
    $ids=implode(",",$tmp);
} 
return $ids; 
}

function get_total_attachments($post_id){
$args=array(
    'post_parent'     => $post_id, 
    'post_mime_type'  => 'image', 
    'post_type'       => 'attachment',
); 
return count(get_children( $args ));
}

add_shortcode('multigallery', 'multi_gallery_shortcode');
1

You can try this function instead of your multi_gallery_shortcode():

function multi_gallery_shortcode($atts, $content=null) {
    extract( shortcode_atts( array(
                'pid' => 0,
        ), $atts ) );

    //format the input
    $pid = intval($pid);

    // construct a post object dependent on the input value
    if($pid>0){
        // query a post object
        $pobj = get_post( $pid );
    }else{
        global $post;
        // current post object
        $pobj = &$post;
    }

    // construct gallery title
    $gallery_title = $pobj->post_title; // customize to your needs

    // construct gallery url
    $gallery_url = ""; // default first image gallery url
    $attributes = wp_get_attachment_image_src( get_first_gallery_image($pobj->ID),'thumbnail'); // customize  to your needs
    if(isset($attributes[0]))
        $gallery_url = $attributes[0];

    // format output:
    $before     = sprintf('<div class="gallery-before"><a href="%s">%s</a></div>', $gallery_url , $gallery_title );
    $gallery_sc = sprintf('[gallery columns="1" order="ASC" orderby="menu_order" include="%s" link="gallery"]',get_random_gallery_images($pobj->ID));
    $after      = sprintf('<span class="galNum">%d</span> Photos.', get_total_attachments($pobj->ID));

    return $before.do_shortcode($gallery_sc).$after;
}

where you use it like this in your posts/pages:

[multigallery pid="1234"]

where pid is the post id.

6
  • Thank you that works, only one thing though, how do i get it to grab the title of the post in which id i used @birgire
    – Mihad Aiko
    Apr 9 '13 at 8:03
  • great, it should get the title via $p->post_title, where $p is either the current post object or the post object with post id $pid.
    – birgire
    Apr 9 '13 at 8:23
  • but it's grabbing from the current post instead of the post id thats attached to the multigallery code @birgire
    – Mihad Aiko
    Apr 9 '13 at 9:10
  • I updated the code
    – birgire
    Apr 9 '13 at 13:59
  • Thank you, but i removed the construct gallery url because it broke the site, but everything else is working fine, THANK YOU :) @birgire
    – Mihad Aiko
    Apr 9 '13 at 16:27

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.