Too many get_template_parts?

In my opinion three or four calls of get_template_part() is perfectly acceptable and even within the norm for some of the larger and more complex themes out there. I don’t know how many is too many but I think it would have to be a pretty large number to notice a hit in performance. A … Read more

Combining shortcode and get_template_part

Try this function get_products($atts) { ob_start(); get_template_part(‘block-products-inline’); return ob_get_clean(); } add_shortcode(‘products’, ‘get_products’); Little explanation php just outputs your content right away when its see print statement. What we do here is, we are holding all the output in buffer and not giving it in print until the whole things finish. then we are returning the … Read more

get_template_part from plugin

/** *Extend WP Core get_template_part() function to load files from the within Plugin directory defined by PLUGIN_DIR_PATH constant * * Load the page to be displayed * from within plugin files directory only * * @uses mec_locate_admin_menu_template() function * * @param $slug * @param null $name */ function mec_get_admin_menu_page($slug, $name = null) { do_action(“mec_get_admin_menu_page_{$slug}”, $slug, … Read more

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