When is get_template_part() preferable to simply using the template.php files?

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 Comment