3

I'm trying to code a theme where I'll have a universal gallery page. Basically like:

http://www.engadget.com/galleries/

However I'm at a loss for what I'd use to call all the galleries. I need the galleries to be custom post types (so I have domain.com/gallery/title-of-gallery/), but the galleries also have to be able to be inserted into posts via [gallery] (or something like that). Also, the galleries have to have some sort of category assigned to them (like Reviews or Previews).

I also need to be able to insert multiple galleries into one post for reviews (where multiple galleries may be needed).

Does anyone have any way I'd be able to achieve this?

  • Is it possible to embed unpublished galleries into a published post? – fuxia Nov 24 '12 at 5:40
  • What if you put a tag in all posts that have a gallery and then do a query for that tag? – brasofilo Nov 24 '12 at 11:27
  • @toscho I'm pretty sure it isn't? I think. But I need the galleries to be published. – user24008 Nov 24 '12 at 15:11
  • @brasofilo Yup, I was reminded about custom post formats by Chip (I was trying to go with custom post types) and I know what to do! – user24008 Nov 24 '12 at 15:12
1

The underlying question here is How do I query all posts with image galleries? (since, once you have such a query, creating a custom template page to loop through them is fairly trivial).

One method would be:

  1. Custom query of attachment posts with a mime_type of image
  2. Loop through them, and add $post->post_parent to an array
  3. Custom query of posts, passing the above array of post IDs as post__in
  4. Loop through them, and output whatever you would like for each

Perhaps like so:

<?php
// Custom query args for image attachments
$image_attachments_query_args = array(
    'post_type' => 'attachment',
    'mime_type' => 'image'
);
// Query image attachments
$image_attachments = new WP_Query( $image_attachments_query_args );

// Loop through them and get parent post IDs
$gallery_post_ids = array();

foreach ( $image_attachments as $image_attachment ) {
    $gallery_post_ids[] = $image_attachment->post_parent;
}

// Custom query args for gallery posts
$gallery_posts_query_args = array(
    'post__in' => $gallery_post_ids
);

// Query gallery posts
$gallery_posts = new WP_Query( $gallery_posts_query_args );

// Loop through gallery posts

if ( $gallery_posts->have_posts() ) : while ( $gallery_posts->have_posts() ) : $gallery_posts->the_post();
    // Loop output goes here
endwhile; endif;
wp_reset_postdata();
?>

Note that this pulls all posts with even a single image attachment. You could maybe get fancy when looping through image attachments, and do something like:

// Placeholder array for IDs
$temp_post_ids();

// Final array for gallery post IDs
$gallery_post_ids = array();

// Loop through them and get parent post IDs
foreach ( $image_attachments as $image_attachment ) {
    // Add ID to the placeholder array
    $temp_post_ids[] = $image_attachment->post_parent;
    // If this post ID has multiple image attachments,
    // add it to the gallery posts query;
    // This will prevent posts with only a single
    // attached image from being queried in the next step
    if ( in_array( $image_attachment->post_parent, $temp_post_ids ) ) {
        $gallery_post_ids[] = $image_attachment->post_parent;
    }
}
  • One second. I thought I had a solution but I've only confused myself more. Thanks for the help. Your code put me on the right track, but I'm still missing something critical here. – user24008 Nov 24 '12 at 2:00
  • Well, fire away about what is still confusing. :) – Chip Bennett Nov 24 '12 at 2:03
  • I missed one component of your question: the part about the galleries being both a typical WordPress post gallery and a custom post type. I really don't think that's feasible. – Chip Bennett Nov 24 '12 at 2:05
  • Aha, yes you caught what I was stuck at. I'm back to square one, trying to figure out how to make galleries appear as post types (like link - which is a WordPress powered blog. I'm thinking it may come down to two options: 1. Extensive coding. Make galleries custom post types and then figure out how to make in-post galleries from that, etc. 2. Find some way to fetch galleries globally (sort of like a posts loop), sort them, and give them dedicated pages (which can be done). – user24008 Nov 24 '12 at 6:08
  • Actually. Would I have to result to using the (ugh) NextGEN Gallery plugin? If you've used it before, it could be a thought. I'd rather code these things into the actual theme itself though. – user24008 Nov 24 '12 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy