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?

4 s
4

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.

Leave a Reply

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