Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Could someone please explain to me how this function works? I know what it does but when I look at the source code in the twenty_ten template, I don't get how all loops are being collected in one single loop.php ( I saw that file too).

So how do for instance abstract away a certain common part of template and then reuse it across other templates?

share|improve this question
1  
Try look at codex.wordpress.org/Function_Reference/get_template_part –  wow Feb 21 '11 at 18:12

4 Answers 4

up vote 12 down vote accepted

Some very good introductory answers here.

Basically, get_template_part() allows theme developers to set up an order of specificity of template files. Think of it similarly to specificity as it applies to CSS selectors. When designing something, you want to start with the bare minimum of specificity, so that it can be easily overridden in parts of a design that need individual attention.

So for example, you're styling a blog and you create a loop.php file which works well for marking up posts. But you plan ahead, and you call it in your template files later with additional context specifiers - say, on the index page, you call get_template_part( 'loop', 'index' );, on the single template, you call get_template_part( 'loop', 'single' );, on archive pages, you call get_template_part( 'loop', 'archive' );, and so on. This makes it very easy down the road when you decide to mark up the loop on your archive pages differently from the home page: just create a loop-archive.php template and it'll be used rather than the generic loop.php.

But the magic behind get_template_part() is in the function locate_template(), which checks first the theme directory, then the parent directory (if one exists) for the file named. This is very useful for plugin development. In one of my plugins, I define a custom post type and created a loop template file for that custom post type in my plugin directory. But... I want to allow themes using my plugin to override my markup if they choose. This is where locate_template() really works wonders.

locate_template($template_names, $load = false, $require_once = true )

will look for each of the names in the $template_names array in the stylesheet directory, then in the template directory. Passing 'true' as the $load argument means that it will require the first file found, and will return an empty string if no template file was located. So I can do something like this in my plugin:

if ( '' === locate_template( 'loop-mycustomposttype.php', true, false ) )
    include( 'loop-mycustomposttype.php' );

...which should hopefully make it very easy for theme developers to customize my plugin just by including a file called loop-mycustomposttype.php in their theme.

share|improve this answer
1  
Replace locate_template by this, please. include(locate_template( 'loop-mycustomposttype.php')) This way is possible to pass variables. I've found this here link. It's extremely useful! –  PabloKarzin Jan 9 at 0:52
1  
Doing so it's going to be necessary to change this if also. Like this if ( false === include(locate_template( 'loop-mycustomposttype.php')) ) –  PabloKarzin Jan 9 at 1:03
    
Oh, good point. With the formula I gave, the require or require_once call (from locate_template) is inside a function and so doesn't have access to the current scope. –  goldenapples Jan 10 at 16:53

Not all loops, the main loop. ;-) No matter if you look at your frontpage or a category or whoknowswhat, you'll always have a main loop. The content of that main loop is determined by the query that's been run before your template got called at all.

The loop.php template merely runs over the items in the loop and formats them. See the documentation at the Codex.

If you look at Twenty-Ten's loop.php, you can see that Twenty-Ten then diversifies within that single template file.

get_template_part() merely loads a template part and runs through it. You can just as well extract parts of your loop.php into separate files and replace them by a get_template_part('loop', 'category') and so on calls.

Or you could have a part-template for each individual post in the loop and have your loop.php only call get_template_part('loop','post'); within the while... clause. All up to you.

share|improve this answer

Check this out. I made a slide for you to better understand it.

How does get_template_part work in the TwentyTen Theme?

share|improve this answer
    
Thanks RG. Am looking at your presentation right now - looks pretty useful –  Amit Erandole Mar 27 '11 at 7:15
    
Fantastically detailed presentation! –  icc97 Feb 8 '12 at 14:20
    
Link is now broken. –  mmcglynn Feb 27 '13 at 12:32

From the get_template_part codex:

<?php get_template_part( 'loop', 'index' ); ?>

will do a PHP require() for the first file that exists...

So effectively it will work as if you were requiring another php file.

Update: There is a slight difference to 'require' - It is wrapped inside a function so you must global if you want to pass any variables from your template to your template part.

share|improve this answer

protected by toscho Jul 2 '12 at 21:26

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.