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.

So let's say I run a TV show blog. One day, the show releases to the public a set of 10 screencaps from an upcoming episode. I post that on my blog, either as a post with multiple images, or specifically as a "gallery" post -- either way, readers can view all the image attached to that post as a gallery.

Then, several days and several posts later, they release 10 more screencaps from that same upcoming episode. I want to make a separate post rather than bump the old one, but I also somehow want to be able to display all screencaps (20) from that particular episode in a single gallery. Or at least what would look like a single gallery.

Possible? (Preferably without NextGEN or other plugins... trying to accomplish as much as I can using WordPress' native capabilities before stacking plugins.)

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

With the default [gallery] shortcode there is no easy way to combine different galleries into one. You can either specify two different galleries: one of the current post and one of another post, specified by post ID:

[gallery]
[gallery id=42]

Or you can create a gallery by explicitly specifying all the attachment IDs:

[gallery include="23,39,45"]

Specifying multiple post IDs will not work because gallery_shortcode() uses get_children() to get the attachments, which uses get_posts(), which uses the standard WP_Query class, and this only allows a numeric post_parent value.

However, we can exploit the fact that there is a filter at the top of gallery_shortcode(), that allows plugins to override the default gallery layout. The following example checks for an id parameter with multiple IDs (or the special keyword this), gets all the attachments of these posts, and puts them in an explicit include attribute which is used to call the gallery function again. This allows you to combine different galleries like this: [gallery id=this,8]. You can extend this idea to support other attributes too.

add_filter( 'post_gallery', 'wpse18689_post_gallery', 5, 2 );
function wpse18689_post_gallery( $gallery, $attr )
{
    if ( ! is_array( $attr ) || array_key_exists( 'wpse18689_passed', $attr ) ) {
        return '';
    }
    $attr['wpse18689_passed'] = true;

    if ( false !== strpos( $attr['id'], ',' ) ) {
        $id_attr = shortcode_atts( array(
            'id' => 'this',
            'order' => 'ASC',
            'orderby'    => 'menu_order ID',
        ), $attr );
        $all_attachment_ids = array();
        $post_ids = explode( ',', $id_attr['id'] );
        foreach ( $post_ids as $post_id ) {
            if ( 'this' == $post_id ) {
                $post_id = $GLOBALS['post']->ID;
            }
            $post_attachments = get_children( array(
                'post_parent' => $post_id,
                'post_status' => 'inherit',
                'post_type' => 'attachment',
                'post_mime_type' => 'image',
                'order' => $id_attr['order'],
                'orderby' => $id_attr['orderby'],
            ), ARRAY_A );
            $all_attachment_ids = array_merge( $all_attachment_ids, array_keys( $post_attachments ) );
        }
        $attr['include'] = implode( ',', $all_attachment_ids );
        $attr['id'] = null;
    }

    return gallery_shortcode( $attr );
}
share|improve this answer
    
@Jan I was thinking that perhaps it is possible to re-attach the images to the new Post? I haven't tried this though, so don't know for sure if that would work... –  Piet May 30 '11 at 8:24
    
@Piet: You can only attach an image to one post. This is because the "parent" post ID is stored in the post_parent field of the attachment, and this can only accept one value. There are some plugins that work around this, but they do it in a non-standard way, and not all other code (like the standard [gallery] shortcode) will work with it. –  Jan Fabry May 30 '11 at 8:44
    
@Jan thanks for the explanation, that does indeed make sense :) –  Piet May 30 '11 at 11:23
    
I haven't delved into things like taxonomies yet, but would it be possible to "tag" galleries in a custom taxonomy called "albums" and then somehow call an album that would essentially call all the attachment IDs of all galleries tagged for that album and display it as a single gallery? –  Chris May 30 '11 at 18:22
    
@Chris: While washing the dishes your idea got me thinking. I now wrote a plugin that "hijacks" the default implementation to do an explicit query for the attachments of multiple posts. –  Jan Fabry May 30 '11 at 19:27
show 1 more comment

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.