Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress get_template_part pass variable

Is there a way of passing a variable to get_template_part() in wordpress:

<?php get_template_part( 'element-templates/front', 'top' ); ?> <?php get_template_part( 'element-templates/front', 'main' ); ?> 

In the front-top.php and front-main.php (which the above is calling) I need to access numeric variables (a different one for each section). Is there a way of passing a variable to each of the calls above?

Thank you

like image 789
Wasteland Avatar asked Jul 28 '18 07:07

Wasteland


People also ask

How do I pass a variable by value in WordPress?

As of WordPress 5.5, passing variables via get_template_part is part of core. Starting in WordPress 5.5, the template loading functions will now allow additional arguments to be passed through to the matched template file using a new $args parameter.

How do I pass a parameter in WordPress?

For template tags that can accept parameters, some require them to be in the default PHP style. For these, parameters are passed to a tag function by placing one or more values inside the function's parentheses, or brackets.

What is Get_template_part in WordPress?

Description. 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. If the theme contains no {slug}. php file then no template will be included.

How do I create a template part in WordPress?

Template parts are incomplete pieces of WordPress PHP templates that get pulled out into their own PHP file. Creating a template part is easy, you first start out by creating a new PHP file. For example, we can create a file called template-example. php.


1 Answers

Using WordPress 5.5+

As of WordPress 5.5, passing variables via get_template_part is part of core.

Starting in WordPress 5.5, the template loading functions will now allow additional arguments to be passed through to the matched template file using a new $args parameter.

get_template_part( string $slug, string $name = null, array $args = null  ) 

Example:

<?php  get_template_part( 'template-parts/featured-image', null, array(    'class' => 'featured-home',   'data'  => array(     'size' => 'large',     'is-active' => true,   ))  ); ?> 

and then in the included file (i.e. template-parts/featured-image), you can either just display the variables (as per above example) :

if ( $args['class'] ) {   echo $args['class']; } 

or

echo $args['data']['size']; 

alternatively setup defaults first, using wp_parse_args:

// Setup defaults $array_defaults = array(    'class' => 'featured',   'data' => array(     'size' => 'medium',     'is-active' => false,    ) );   $args = wp_parse_args( $args, $array_defaults );   <div class="widget <?php echo esc_html( $args['class'] ); ?>">     <?php echo esc_html( $args['data']['size'] ); ?> </div> 

To be backwards compatible in your theme, you should probably also check the current WordPress version.

Using set_query_vars

The original answer to this questions was to use set_query_var

In your theme:

<?php set_query_var( 'my_var_name', 'my_var_value' ); get_template_part( 'template-parts/contact' ); ?> 

In the template part:

<?php $newValue = get_query_var( 'my_var_name' ); if ( $newValue ) {   // do something } ?> 
like image 148
thetwopct Avatar answered Oct 14 '22 12:10

thetwopct