I See get_template_part in my WordPress Theme. What’s it For? How Do I Use It?

I have seen quite a few searches come through lately for get_template_part.  Seems like a lot of folks don’t know what it’s for, what it does, or how to use it in their WordPress theme.  When it was introduced with WordPress version 3.0 I wasn’t all that excited.  I didn’t see a need for it.  I was kind of set in my ways.  I liked how I did things.  But after looking into it for a while, I discovered it’s a powerful and useful tool.  Let’s take a look at what it does for you.

First off, what is it?  Simply put, get_template_part is just a way to include another file.  But it’s more powerful than a php include statement, because of its flexibility.  It’s amazingly useful, especially for anyone trying to build a child theme.  So let’s take a look at how it could work for you if you are trying to make a child theme for twentyten first.  If we look at index.php in twentyten, we see:

get_template_part( 'loop', 'index' );

What’s that do?  Well let’s take a look at the structure.  Get template part follows the format of:

get_template_part( $slug, $name );

Where the slug is the generic name of the template being called, and name is the specialized name of the template being called.  So there is an order that WordPress would use to try to find the template to use.  Properly named files in your child theme come first, then files in your parent theme are used.  So using the example above with the generic name ‘loop’ and the specialized name ‘index’ here is the order WordPress would look for files.

  1. wp-content/themes/twentytenchild/loop-index.php
  2. wp-content/themes/twentytenchild/loop.php
  3. wp-content/themes/twentyten/loop-index.php
  4. wp-content/themes/twentyten/loop.php

See how that works?  We look for the specialized template in your child theme.  Then the generic.  Then the specialized name in the parent template.  Finally the generic name in the parent theme.

Now sticking with the twentyten theme. We already know we have this in index.php:

get_template_part( 'loop', 'index' );

And we also can see we have this in archive.php:

get_template_part( 'loop', 'archive' );

So what does it all do?  Well, in a basic twentyten install, there are no specialized templates, nothing named loop-index.php or loop-archive.php, so WordPress simply uses loop.php.  But the power comes in here for you, the child themer!  You could overwrite the loop for the index page or the archive page by creating the specialized named file loop-index.php or loop-archive.php.  For instance, if you only made a loop-index.php, then your file would be used for the main index page.  But since you didn’t make a loop-archive.php, twentyten’s original loop.php file would be used.  You see, you can override either template, without affecting the other.  Now if you made a file called just loop.php in your child theme, with no specialized template, then your loop.php would override twentyten’s for both situations.

Hopefully now you can see the naming conventions. How it all works, and how you can override templates in themes which use get_template_part. It gives you a lot of power when making a child theme. But what if you are making your own theme? How can you use it? Well you could a couple of routes here. In all your files (index.php, archive.php, category.php, etc) You could make a call to get_template_part, and make an individual file for each loop. So index.php would have:

get_template_part( 'loop', 'index' );

And then you would make a file called loop-index.php with the loop in it. You could do that for each file. This now gives anyone trying to make a child theme for your theme a way to override your loops. Or you can do what I like. Use get template part in your theme, but just make a loop.php file and code it to account for everything. I have a giant loop.php which uses a variety of conditional statements to cover all my templates. It handles index, archive, my shop, pages, single view, custom post types, etc. I just built the file up as I added everything. So now I have one big loop, but have given users of my theme the ability to use their own loops with get_template_part.

You can also use get_template_part to call in things besides the loop. For instance I use:

get_template_part( 'icons', 'social' );

to call in a file called icons.php. It’s a big block of code that I use to display social icons under member profiles on my blog. Rather than repeat the code on various templates I just included it. This is nice for me as it allows me to reuse the block of code without actually typing it into each template. Keeps things neat and organized. But it also allows child themers to override it with their own code if they feel the need.

That’s it for this article. Hopefully it helped you understand what that bit of code is doing, and why get_template_part is so useful. Now get to building your own child theme, and have some fun with it!

Leave a Reply

Your email address will not be published. Required fields are marked *