Premium Theme Development: Using get_template_part() for Switching Blog Layouts

This is post 1 of my Premium Theme Development post series, this post covers the question “How do you developers go about template switching”, which was asked by Level_1_Media in the Themeforest forums.

What this answer covers, is, how can we switch blog layouts within a WordPress Theme? Well the answer is really simple, like this:

get_template_part( 'loop', get_option('blog_layout', 'masonry') );

But what does that do, why and how should I use it?!

Ok, fair enough, the above doesn’t cover the question in any detail really.

Let’s use a working example then, one of my latest WordPress themes, Zonya, has an option for multiple blog layouts, a masonry blog layout, and a more classic blog layout. So the question is, how am I letting users manage these? Well the answer is very simple, you can see it above, but let’s cover it in more depth.

1. What is get_template_part()?

get_template_part() is an amazing function in WordPress that allows you to easily turn your files into “chunks”, so I can take my entire blog loop and its surrounding markup, and chunk it off into it’s own file. This serves a few functions:

  1. It keeps my code really really clean.
  2. It’s totally child theme compatible, so a buyer can easily overwrite this file from their own child theme with no additional function calls (like do_action())
  3. We can load this parts variably to tie into theme options, post_meta and similar.

 2. How does this apply to creating multiple blog layouts?

Simple. As I said before, I can take an entire blog loop and it’s surrounding markup and move it to my own file, then, if I have another blog layout, I can do the same to give me 2 possible blog loops. These 2 loops can then be selectively loaded via the piece of code we started this article off with:

get_template_part( 'loop', get_option('blog_layout', 'masonry') );

Perhaps this line of code is starting to make more sense now? Maybe not, I’m not so great at writing, so I’ll continue to really hammer the point home.

Creating the Multiple Blog Layouts of Zonya

Ok, so to start, I have an extremely organised header.php that contains all my usual header markup, but also all the surrounding containers for building my page, I also have an organised footer.php with the usual stuff and the closing parts of all my containers, so my index.php is going to be small. Remember that index.php should only ever be used for the blog index, so this is a great place for us to switch our layouts from. Here’s the index.php from Zonya:


 * index.php
 * The main post loop in Zonya
 * @author TommusRhodus
 * @package Zonya
 * @since 1.0.0

 * Get the blog layout
get_template_part('loop/loop', get_option('blog_layout', 'blog-grid-sidebar') );


So in my index.php (remember, the main posts archive) we grab the header, then we selectively grab a blog loop (depending on a Theme Option, i’ll cover this in another post) and then we grab the footer to close it all off.

Now I do this because my 2 blog layouts have wildly different markup so it makes the most sense to work like this. If you have just 1 blog layout but want to give your buyers the ability to switch between say a 2 column layout and a 3 column layout, then you could do that without get_template_part() and just applying your theme option as a variable class instead.

Finishing this up, and making sense of it

From the index.php example above, I have 2 files:

  1. /loop/loop-masonry.php
  2. /loop/loop-classic.php

We’re using a theme option to switch between the two (remember the piece of code from the beginning of the article) like this:

get_template_part( 'loop', get_option('blog_layout', 'masonry') );

But the same can be done by assigning a variable in any way you wish:

get_template_part( 'loop', $blog_layout );

Or you could just call your blog layout directly:

get_template_part( 'loop', 'masonry' );

But from a Premium Theme point of view, you’ll be giving this option to your buyers via a theme option from the options panel of your choice, in which case, if your markup is different between your 2 blog loops, you should use get_template_part() to switch between the loops depending on your buyers choice.

I hope this rambling helped in any kind of way.