How to pass variable through get_template_part()

Hi everyone,

I have this piece of chode

<?php
if ( has_post_format( 'quote' )) { ?>

	<?php get_template_part('includes/post-formats/quote-format.php'); ?>

<?php
} else if (has_post_format('gallery')) { ?>

	<?php get_template_part('includes/post-formats/gallery', 'format'); ?>

<?php
} else if (has_post_format('video')) {
   // stuff to display the link format post here
} else { ?>

	<?php get_template_part('includes/post-formats/clasic', 'format'); ?>

<?php } ?>

and in each particular template part I’m using $item_width variable. Does anyone know how to pass this variable through these functions?

Thank you very much for all advices and suggestions :slight_smile:

There is no way, either have your variables globally accessible, or use this instead:

include( locate_template( 'template-part.php' ) );
OriginalEXE said

There is no way, either have your variables globally accessible, or use this instead:

include( locate_template( 'template-part.php' ) );

Thank you very much for your reply :slight_smile:

I’ve been searching for a solution to send variable in “get_template_part” function and always without luck. I personally don’t like to use global variables because I know, this is just a bad practice. A few months ago I’ve been able to design a solution that pass Theme Check and has support for child themes. I think is a good idea to share with TF authors.

Here it is: https://github.com/Smartik89/SMK-Theme-View/blob/master/functions.php

Use it like this:

smk_get_template_part('filename.php', array(
   'variable_1' => 'Content',
   'other_variable' => 'Other content'
));

And access the variables in filename.php like this(note the $this-> prefix):

echo $this->variable_1; // Output `Content`
echo $this->other_variable; // Output `Other content`

Happy coding.

Smartik why dont use include( locate_template( ‘template-part.php’ ) ); ? It is simplest…

Why not adding properties to the global post object?

Parent:

<?php global $post; $post->myvar = "hello"; ?>

included file:

<?php global $post; echo $post->myvar; //returns "hello" ?>
jachu said

Smartik why dont use include( locate_template( ‘template-part.php’ ) ); ? It is simplest…

Check the code that I’ve shared. I use include and I do also some additional checks to make sure it does not break anything. Also usign direct include is not allowed by ThemeForest because Theme Check will detect it as a warning.

QantumThemes said

Why not adding properties to the global post object?

Parent:

<?php global $post; $post->myvar = "hello"; ?>

included file:

<?php global $post; echo $post->myvar; //returns "hello" ?>

Because you should not use global variables: http://smartik.ws/2014/07/do-not-use-php-global-variables-never/

A posible solution is this
ob_start();
smk_get_template_part(
LOCATION OF YOUR FILE OR TEMPLATE,
array(
‘data_1’ => $variable,
'data_2 => 3,
‘data_3’ => ‘someone here’
)
);
$variable_template = ob_get_contents();
ob_end_clean();

Old Topic