Why do we pass a variable to the get template part function?

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.

Why do we use get template part function in WordPress?

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. Or we can simply have what could have been a variable declarion stand as a function-call inline, like this line from the Twenty Nineteen theme:

Where are the variables in a template expression?

Within a template expression, you have access to the parameters context that contains the values of parameters passed in. Additionally, you have access to the variables context that contains all the variables specified in the YAML file plus many of the predefined variables (noted on each variable in that topic).

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.

How to load a template part into a template?

Loads a template part into a template. Provides a simple mechanism for child themes to overload reusable sections of code in the theme. Includes the named template part for a theme or if a name is specified then a specialised part will be included.

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.