Contents
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 are the benefits of using document templates?
Document templates simplify the duty of making documents through offering a framework for any files you need. Through using these templates, you are able to effortlessly personalize a document to suit your needs.
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.
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.
The get_template_part function simply builds an array of templates for locate_template to look for. Here’s a quick example: Creates an array of “one-two.php” and “one.php” (in that specific order) and passes it on to locate_template, which then loops through that array and looks for the files in the child and parent themes directories.
What does get _ template _ part ( ) do in WordPress?
A workaround – using include () and locate_template () If you dig deeper into WordPress code you will see that get_template_part () actually uses another function called locate_template () which is basically used to detect if a template exists in child theme.
How to pass variables to get _ template _ part?
Optionally, locate_template () may load template (and clear all variables as get_template_part) or it may just return the path to an actual file without loading it. Combining locate_template () and native PHP include () function, we get exactly what we need.
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.
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 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.