Call me slow – but I only just today discovered that WordPress has some built-in photo gallery functionality!
I’ve been using the NextGen plugin for quite some time now – but often the feature set is just too much for requirements.
I’m not sure if this is required, but I noticed it’s in the TwentyTen theme so I did it anyway. Open your theme’s functions.php file and add:
[php]add_theme_support( ‘post-formats’, ‘gallery’ );[/php]
You’ll notice if you go to edit a post (or a page) and upload some images, on the gallery tab you can insert… a gallery! You can even sort the images via drag and drop – very simple yet effective.
By default, WordPress wants to link to an “attachment page” rather than provide a pop-up image gallery. I couldn’t find a way around this, so I set about customising what the “attachment page” looks like.
Create a new file in your theme folder called image.php
Here’s my basic file to start with:
[php]<?php get_header(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
$size = 600; // x value
$size = 600; // y value
// or you can use “medium” or “large” and this will use the values in the Media Settings
$postid = get_the_ID();
echo wp_get_attachment_image( $postid , $size ); ?>
<?php the_excerpt(); // the image caption ?>
<?php the_content(); // the image description ?>
<?php endwhile; else: ?>
<p><?php _e(‘Sorry, no posts matched your criteria.’); ?></p>
<?php endif; ?>
<?php get_footer(); ?>[/php]
The first couple of lines here get the header and loop started, as per any other template page in your theme.
The title comes directly from your image title when you upload it. Similarly, caption and description are stored in the_excerpt and the_content respectively.
In my example here I have specified x and y values (lines 13 and 14) which you can change according to the column width in your theme – but it’s probably better to set these values in the Media Settings and then use the keyword “medium” or “large”. This will resize the image on upload, and then display this previously-resized image. (The example I have used here just grabs the full-sized image and uses HTML attributes to display it in the browser. Not so good.)
One Last Thing
The standard method WordPress uses to display the gallery thumbnails seems to suffice, but if you want to disable that and style the whole thing yourself, add this to your theme’s functions.php:
[php]add_filter( ‘use_default_gallery_style’, ‘__return_false’ );[/php]