Contents
- 1 When to use get template part in WordPress?
- 2 Where can I find the get template part?
- 3 What does get _ template _ part ( ) do in PHP?
- 4 Why do we pass a variable to the get template part function?
- 5 What should the name of the template part be?
- 6 How does the get template part function work?
- 7 Is there a third parameter for get template part?
- 8 How to retrieve page template in parent template?
When to use get template part in WordPress?
In WordPress, get_template_part () is a helpful way to avoid duplication and repetition within a WordPress theme. Use get_template_part () for any template code that you would otherwise end up copying-and-pasting.
Where can I find the get template part?
At the time of writing, get_template_part is located in wp-includes/general-template.php and locate_template is in wp-includes/template.php. Well, that’s about it folks! You should now be the master of the get_template_part voodoo, and now that you’re using it in your themes, you should get approximately 74% more sales.
What does get _ template _ part ( ) do in PHP?
With get_template_part (), the child’s content.php can simply replace the parent’s—but since include () and require () pull in files in a “non-WordPress” way, the child theme must instead replace every theme file that uses these template parts, creating huge unnecessary bulk in the child theme.
What is the power of get template part?
The real power of get_template_part comes from a function called locate_template, which does the whole searching in parent theme and child theme folders, and the reverting to other templates in a stack. The get_template_part function simply builds an array of templates for locate_template to look for.
Can you include VAR in locate _ template ( )?
But locate_template () infact does no inclusion at all. It just locates a file for inclusion. So you can make use of include to have this working just like you expect it: $var from your example can be used in the template part then.
Why do we pass a variable to the get template part function?
With get_template_part, passing variables to the second argument is also rather common. Passing variables to get_template_part is useful because it allow us to have things (like the WordPress post format) stored as a variable and then passed in to the function.
What should the name of the template part be?
One thing to note from this example: content.php is an arbitrary name—you can name your template parts anything you want. get_template_part ( ‘anything’ ); would work perfectly fine, as long as the theme (or its child or parent theme) contains a file called anything.php. That name, specifically, we don’t recommend.
How does the get template part function work?
By using the get_template_part function to include all the elements individually, our customers are able to modify one element at a time without having to worry about losing out on the benefits of regular theme updates.
What does get template part ( ) do in PHP?
The get_template_part () function acts like a PHP include () or require (), but with a few extra bells and whistles. You can actually follow the function paths backwards in core (see below) to the load_template () function where you find the require () and require_once () functions being used.
What happens if there is no template in WordPress theme?
If the theme contains no {slug}.php file then no template will be included. The template is included using require, not require_once, so you may include the same template part multiple times. For the $name parameter, if the file is called ” {slug}-special.php” then specify “special”. (string) (Required) The slug name for the generic template.
Is there a third parameter for get template part?
As pointed out in the comments, current versions of WP offer a third parameter for get_template_part (): array $args. So from this version on, you do not need to use set_query_var ( ‘foo’, ‘bar’ ) anymore.
How to retrieve page template in parent template?
Retrieve path of page template in current or parent template. The hierarchy for this template looks like: {Page Template}.php The template hierarchy and template path are filterable via the ‘$type_template_hierarchy’ and ‘$type_template’ dynamic hooks, where $type is ‘page’.