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.

In this post on wordpress.stackexchange.com I asked whether using get_template_part(), as demonstrated in the TwentyTen theme, is a recommended best practice. The general consensus I got was that it was not necessarily the best practice in all situations.

This related question then is: can you provide me with an example where using get_template_part() would be the recommended approach, over simply defining the separate template.php files?

For example, I can either define archive.php, single.php and page.php; or I can define loop-archive.php, loop-single.php and loop-page.php.

In what circumstances would it be preferable to use get_template_part('loop', 'single') and leverage loop-single.php versus simply defining single.php?

share|improve this question
    
Just to add back, now that I've been using it for some time, I'm finding that it's really great to use get_template_part() for the actual atomic "content". For example, suppose you have a custom post type called "product". You might have one template that lists all your product, another that shows the detail page for your product, another that shows all product in a specific category. get_template_part('content', 'product') is a nice way to encapsulate all the product-specific content and formatting, to be reused in a variety of templates. –  Tom Auger Feb 6 '12 at 18:17

4 Answers 4

up vote 3 down vote accepted

A recommended approach for using get_template_part would be for including bits of code that would otherwise be repeated frequently in all your templates. Like if you had conditionals defined within your loop that you wanted to include in archive.php, search.php, single.php etc.

It also allows child themes to override that file and include additional more specific files. For example if you used get_template_part( 'loop', 'single' ) and your theme only has a file named loop.php then a child theme could include a file named loop-single.php that would override your loop.php.

It's basically a custom includes tag for template files other than header, sidebar, or footer.

share|improve this answer

I don't feel like there is ever a valid reason NOT to use get_template part. Before it existed, WordPress themes were filled with repeated code and were very time-consuming + confusing to make larger modifications. Having one loop file that is always used to display posts is a godsend both for child themes and those who wish to "fork" themes. If you look at the code for the Twenty Eleven theme (currently in development) you will see that template parts have been extended into post formats as well. You will also notice that "The Loop" has been removed from the template and placed in the calling template. I think this is the best ways to use template parts for post content and is much more flexible than Twenty Ten.

Did you know that other template behave almost identically to get_template_part()? I wrote and article explaining how: http://wordpress.mfields.org/2011/adding-context-to-template-parts/

IMHO the more flexible a parent theme is the better!

share|improve this answer

get_template_part() should (imho) be used if you use that code in more than one template. For ex. a header, a nav menu or a sidebar.

Templates should (imho - again) be used if a template is different in more than just some lines from the rest of the templates.

I personally only use a simple index.php that includes conditional tags to load my query specific template parts in my small base theme. In the larger theme i got only hooks in my index.php file and hook in my template parts conditionally (but the setup is far more complex - mostly real cms stuff).

share|improve this answer

Building on Kaiser's answer, get_template_part() is useful for repetitive code. For example, most of your main template parts probably have a very similar loop. Putting that loop into a file called loop.php and calling get_template_part('loop'); means that, the next time you want to change the code in your site's loop, you do it once in one place, rather than the dozen or so times in the dozen or so template files.

That's one huge advantage of get_template_part(). The other one, and the more important one, in my opinion, is that it works very well with parent/child themes. Using the loop.php example again, if you have a child theme and wanted to modify the loop code in your child theme, but didn't want to change the rest of the template, you would only need to override the one loop.php file in your child theme, rather than the dozen or so other template files.

share|improve this answer
    
Thanks John. I chose Chris' answer arbitrarily (yours are very similar) because he posted it 1 minute before you. :) –  Tom Auger May 12 '11 at 19:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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