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"></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');
You can try this function instead of your
multi_gallery_shortcode()
:where you use it like this in your posts/pages:
where
pid
is the post id.