WordPress Built-in Gallery Functionality

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.

Step One

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.

Photo gallery

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.

Step Two

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(); ?>

<h1>
<?php the_title(); ?>
</h1>

<div>

<?php
$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 ); ?>
<div>
<?php the_excerpt(); // the image caption ?>
</div>
</div>

<?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.

Image upload fields

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]

Advertisements

2 thoughts on “WordPress Built-in Gallery Functionality

  1. I’m sorry to disturb you
    but in a old post you tell how to had the description of the gallery
    with nextgen gallery
    the post is
    http://www.rockyshark.com/archives/2009/03/displaying-a-gallery-description-using-nextgen-gallery-for-wordpress/
    it s a very good post but the comments are closed
    I would like to know how to hide the description of the gallery for the next page of the gallery

    description); ?>

    lio

    but it didn t work
    please help me I m lost
    thank you very much

  2. Lionel,

    I think that using the built-in functionality is probably the best way to go moving forward – it’s much simpler to deal with.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s