Mixing Gallery and Blog Posts

This is part 3 on using the built in WordPress Gallery features. Part 1 and Part 2 are recommended reading also.

So, you’ve created a few galleries, you’ve got your single image page looking spot on with all the details and EXIF data displaying. However when you create a new gallery what happens on the front page of your site? If you’re display the full post, the whole gallery will display. If you’re displaying an excerpt, you’ll get a short excerpt if you’ve set one. Personally I like the way Matt Mullenweg has set his blog up. You get the full post if it’s a standard blog post, but if it’s a new gallery it will display a thumbnail from the gallery, along with a short excerpt and the number of photos in that gallery.

So how can we mix these together on the front page? First off we need to ensure that the code we put in place only affects the gallery posts and not the standard posts. So this is easily done by checking which category the post is in:

<?php if (in_category('3')) {
// add our code for dealing with a gallery post here
} else {
the_content();
} ?>

Note, the ID of 3 used above is the category ID. You’ll need to check your categories and replace 3 with the ID of your gallery category.

Now we need to work out the code.

Retrieve a Photo

To get a photo we take the ID of the gallery post and use the get_posts() function to retrieve the images associated with that post. This is relatively easy and the following code will get one image randomly for us:

$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID,
'orderby' => 'rand'
);
$attachments = get_posts($args);
foreach ($attachments AS $attachment) {
// code to display the image goes here
}

The above code will randomly pull out just 1 attachment connected to the post currently being displayed in the loop. We then use the foreach() loop to retrieve the information from the $attachments array. We can retrieve any information from the database record for this image, however we just need the URL of the image, the title of the image and its record ID ie.

$alttxt = $attachment->post_title;
$imgid = $attachment->ID;
$fileurl = $attachment->guid;

Unfortunately that’s not all there is to it for getting the thumbnail. The $fileurl value will contain the original image’s URL, ie. our large version. We need to display the thumbnail, not the large file. When an image is uploaded it will be resized into a medium and thumbnail size. All 3 files are stored in the same location as

  • Original: filename.jpg
  • Medium: filename-600×400.jpg
  • Thumbnail: filename-150×100.jpg

The second two filenames are basically filename-widthxheight.jpg so these figures will depend on your settings under Options – Miscellaneous.

So, we need to make filename.jpg become filename-AxB.jpg where A and B are the width and height of your thumbnail. Luckily we can use the EXIF data to work out the size of the thumbnail and added that into our image filename:

$meta = wp_get_attachment_metadata($imgid);
$imgw = $meta['sizes']['thumbnail']['width'];
$imgh = $meta['sizes']['thumbnail']['height'];
// now get the file extension and take it off our file url
$imgext = substr($fileurl, -4);
$fileurl = substr($fileurl, 0, -4);
// and now put it all together
$fileurl = $fileurl."-".$imgw."x".$imgh.$imgext

It’s a bit messy, but it will do the job. Whilst you can get the thumbnail file path directly from the EXIF data, this is the full file path and not the URL path, so at present the above is the best way I’ve found.

Count the Photos

It’s nice to be able to say how many photos are in the gallery, and this is easily done with a slight code change to the above. Instead of retrieving just 1 attachment we can retrieve them all, count how many are returned, but force the foreach() loop to only display 1. To retrieve all the attachments we change the line in our arguments array for the get_posts() function to

'numberposts' => -1,

Then to get the number of attachments returned we use the PHP array count() function once the get_posts() function as been executed:

$noimages = count($attachments);

The last thing we’ll need to do is make the foreach() loop only run once, so we use the break; command, which will break out of the loop.

The Final Code

So putting all of this together we get the following code:

[sourcecode language=”php”]< ?php if (in_category('3')) : // is a gallery post so alter the display $args = array( 'post_type' => ‘attachment’,
‘numberposts’ => -1,
‘post_status’ => null,
‘post_parent’ => $post->ID,
‘orderby’ => ‘rand’
);
$attachments = get_posts($args);
$noimages = count($attachments);

if ($attachments) {
?>

17 Comments

  1. Richard Bui August 3, 2008
  2. willy August 3, 2008
  3. Sarah August 3, 2008
  4. slee August 4, 2008
  5. Sarah August 4, 2008
  6. slee August 4, 2008
  7. Sarah August 4, 2008
  8. Grant H August 11, 2008
  9. Sarah August 12, 2008
  10. Grant H August 12, 2008
  11. Sarah August 12, 2008
  12. Roger Coathup September 18, 2008
  13. Roger Coathup September 20, 2008
  14. Sarah September 20, 2008
  15. kristarella November 18, 2008
  16. Sarah November 19, 2008
  17. nitgreen January 5, 2011