How to add custom content template part for a custom post type on main query using a plugin

Background Unfortunately get_template_part() function doesn’t have any suitable filter to achieve what you want. It’s possible to use the get_template_part_{$slug} action hook to inject template parts, however, without any change to your theme or a child theme, the original template part will be added anyway. So this way you’ll not be able to replace existing … Read more

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 … Read more

How to make get_template_part always check child theme first?

It does, by default. The get_template_part() function uses locate_template() which cascades through the template files in in order of specificity and stylesheetpath/templatepath. So, if your Child Theme includes a content-inventory.php, then get_template_part() will include it; if not, then it will look for content-inventory.php in the parent Theme. If it doesn’t find it, it will then … Read more

Is there a variable for a template parts name?

There isn’t a core global variable that returns the current context. However, you can construct your own, using contextual template conditional tags. You can step through the conditional tags in the same order as WordPress core, by following wp-includes/template-loader.php. Simply wrap your output in a custom Theme function. Here’s how I do it (note: I … Read more

Should we use get_template_part() in functions files instead of include_once?

Your functions.php doesn’t create output, so you should use locate_template(). Example: locate_template( ‘php/functions.nav-menu.php’, TRUE, TRUE ); You’ll find this function in wp-includes/theme.php. The first parameter is the file path relative to the theme root, the second tells WordPress to load it (or not), and the third to load it just once. Now a child theme … Read more

get_template_part vs action hooks in themes

I prefer hooks, since they are more flexible: you can hook into them from your theme’s functions.php file, but also from plugins. I try to put as much logic in plugins, so that the themes contain mostly layout stuff. If you use an action hook, it is still possible to use get_template_part() in that hook … Read more

How to include a file using get_template_part() in a plugin?

get_template_part is a theme function. You can’t load plugin files with that function. Take a look at the source and you will notice the work is done by locate_template. Look at that source and you will see that it always loads from theme directories. However much you may want to use get_template_part it is the … Read more