Get $500+ of the best After Effects files, video templates and music for only $20!
Creating Your Own Image Gallery Page Template in WordPress

Creating Your Own Image Gallery Page Template in WordPress

Tutorial Details
  • Program: WordPress
  • Version: 3.0+
  • Difficulty: Medium
  • Estimated Completion Time: 45 minutes

Final Product What You'll Be Creating

Today I’m going to walk you through creating a custom template for “Gallery” pages in WordPress. I remember in the past wanting to implement a gallery into my theme and getting frustrated with the options. WordPress’ built in gallery works well, but doesn’t quite fit the bill most of the time. Additionally, plugins like NexGen gallery are often more than you need or want. I will show you how easy it is to create a gallery template page that you could easily tweak and modify for future themes or versions.


Overview

Through the tutorial I’m going to show you how to create a gallery template on the theme included with WordPress, TwentyEleven. You can use any theme you want but for this example’s sake I’m using TwentyEleven.


Step 1 Duplicate the Existing Page Template

Go into the theme directory, wp-content/themes/{theme} that you want to add the gallery template to. Find page.php and copy and paste and rename to template-gallery.php. For custom templates I like adding the prefix ‘template-’ as it makes it easier to find.


Step 2 Define the Gallery Template

WordPress recognizes template definitions in the header of the themes template files. To define the template you add a PHP comment like the example below. To do that open the template-gallery.php that you just created and add the comment block what specifies the template name.

	<?php
	/*
	Template Name: Gallery Page
	*/
	?>

http://codex.wordpress.org/Theme_Development#Custom_Page_Templates


Step 3 Edit Template to Load Page Images

WordPress processes attachments to pages as posts post_type – attachment with the page as the parent. WordPress has a built in function to query posts that are children of a specific page post/post_type. get_children(); accepts all sorts of parameters for pulling children content associated to a parent. Check out documentation at http://codex.wordpress.org/Function_Reference/get_children. This WordPress function allows us to easily pull attachments for a specific page.

For our gallery, we want the images to appear before the content. So we are going to load the page images in the page loop but before the content. For TwentyEleven you can place the snippet right below <?php the_post(); ?>. In other themes you can insert the code in the loop above <?php the_title(); ?>. Check out the code below to see how get_children( $args ); is implemented and read the explanation after it.

	<?php

	$args = array(
		'numberposts' => -1, // Using -1 loads all posts
		'orderby' => 'menu_order', // This ensures images are in the order set in the page media manager
		'order'=> 'ASC',
		'post_mime_type' => 'image', // Make sure it doesn't pull other resources, like videos
		'post_parent' => $post->ID, // Important part - ensures the associated images are loaded
		'post_status' => null,
		'post_type' => 'attachment'
	);

	$images = get_children( $args );
	// continued below ...
	?>

Explanation of $args

  1. ‘numberposts’ We can define the amount of images the function pulls. To query all images, use -1
  2. ‘orderby’ We could order by title or date or another option, but the benefit of ‘menu_order’ is that the order in the media manager for the page will be the order the images are loaded.
  3. ‘post_mime_type’ We only want the posts that are ‘images’
  4. ‘post_parent’ We want to pull the children of the current page. We can access the global $post and it’s property ID to pass in as the parent_id
  5. ‘post_type’ ‘post_mime_type’ takes care of making sure only images are pulled, but defining the ‘post_type’ as ‘attachment’ helps slim down the query.

Verify the post has images and loop

After get_children( $args ); queries up attachments and returns values we want to verify that the query returned results. Some pages may not have images attached and we don’t need to loop through empty results. We’ll use a conditional to see if $images returns a value, and if it does, then we know the page has attachments and then we can loop through them.

	// continued from above ...
	if($images){ ?>
	<div id="slider">
		<?php foreach($images as $image){ ?>
		<img src="<?php echo $image->guid; ?>" alt="<?php echo $image->post_title; ?>" title="<?php echo $image->post_title; ?>" />
		<?php	} ?>
	</div>
  	<?php } ?>

For each image attachment we want to output the image. Each $image object has several properties you can access and output. The most important for this example is the source of the each image, which is the guid property. For accesibility and SEO purposes we can ouput the image title and place in the image alt & title attributes of the images.


Step 4 Add the Javascript Gallery Plugin

For the next step we are going to add a jQuery plugin to bring our gallery to life. The plugin we are going to use is Flux Slider, a slider Javascript/jQuery plugin. Download the source and place the flux.min.js inside the wp-content/themes/{theme}/js folder. Since I am using the TwentyEleven theme, jQuery is not included on the public side, so I will need to make sure to include it – if you are using a different theme, make sure jQuery is loaded.

Edit footer.php

Include jQuery (if not aleady included) and the flux.min.js script in the footer – make sure flux.min.js is below the jQuery inclusion but before wp_footer(); function.

	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
	<script src="<?php bloginfo('template_url');?>/js/flux.min.js"></script>
	<script>
		window._flux = new flux.slider('#slider',{pagination: true});
	</script>

	<?php wp_footer(); ?>

The End

As you can see, it’s pretty easy to pull images attached to a page and customize their output. You could easily include this same loop in posts or a custom post type.

You could easily swap the jQuery plugin for another option of your choice. All you have to do is edit the loop HTML with the appropriate markup and include the plugin.

WordPress is very flexible, and this is another example of how easy it is to build with it!

Add Comment

Discussion 21 Comments

  1. Ricardo Ribeiro says:

    Nice tutorial. But why not provide a demo?

  2. pointbreakid says:

    Now Wp Tutsplus Can’t provide a Demo that just Image and really Confused and not interest the visitor

  3. Paul says:

    Nice tutorial and thanks for introducing to the flux slider.

  4. Liton Arefin says:

    I’ve searched for few days “How to setup any sliders in WordPress Homepage” for making a wordpress premium like theme. But i didn’t get enough resources for it. Please i want a full tutorial about this topics. Step by step that i can setup any slider in my wordpress theme. Also i want to show the five recent posts in slider. Hope you’ll not disappoint me.

    Thank you.

  5. Ankur says:

    Nice Tut. I am implementing it on my site

  6. Shawn says:

    I’d like to see a demo page, but after I try it myself I’ll have one anyways. Nice tutorial.

  7. Travis says:

    Is there any reason you’re not using wp_enqueue_scripts to include the javascript files?

  8. wp lovr says:

    Yup, without the demo it doesn’t feel live, but I found a simple flux demo here.http://www.script-tutorials.com/demos/94/index.html

  9. al says:

    I really do suggest that articles like this list the browser requirements for them.

    I run XP and use IE8 most of the time and when I look at documentation for this slider, I get a message saying this slider requires the ability to handle CSS3 properties, obviously IE8 does not fit that category. I usually then check it out with FF and Chrome to see how the function should work.

    not a big problem but I do wish the pre-reqs for the article would be listed. you list WP 3.0+, add browsers as well.

    Al

  10. Author

    I totally get that a demo would be nice, and I agree! But the emphasis is on demonstrating how to load images that are attached to the page. You could easily follow these same steps and use any gallery type jQuery plugin and simply adjust the HTML (if needed) per jQuery plugin requirements.

  11. Raoni says:

    Hi…is “numberposts” deprecated??

  12. plainflavour says:

    Nice tutorial, I’d been struggling with this.

    I can get it to work for gallery images, but it doesn’t seem to pull in custom field images. Do you know of a way to loop through those as well?

    Thanks

  13. Great tutorial for beginners! Well written.

    I would actually encourage expanding on this. Maybe using the image control section as a template part instead of a page template. That would allow you to add the gallery on any page with ease.

    If you really wanted to make it steller you would turn it into a PHP class it make it unbelievably portable!

  14. Paul Weston says:

    Thought this was a great tutorial. I am new to WordPress and have only just started to use for my design work. I am still getting my head around WordPress but found this tutorial very interesting and through the detail of the tutorial I am picking up the coding for WordPress more and more. This is a tutorial I will be trying out and one that will help me for my future projects.

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.