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
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.
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.
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.
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.
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.
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 } ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With