• Hi,

    I`m trying to load a variable from another template, but it won’t work. I Googled around, but i couldn’t find an answer, so i really need your help!

    This what i try to do for example:

    Templatefile-array.php
    <?php $fruit = 'apple'; ?>
    
    Templatefile-load-array.php
    <?php locate_template('Templatefile-array.php', true);?>
    <?php echo $fruit;?>

    The expected outcome would be ‘apple’.
    But nothing shows up….
    It normally works fine in PHP.

    Can someone please tell me how to solve this?
    Thanks!

    Kind Regards,

    Yourit

Viewing 3 replies - 1 through 3 (of 3 total)
  • Are you getting the template name back as a return value if you set load to false, ie. the second parameter to false?

    Eg.

    $template_to_locate = locate_template( array('somefile') );
    echo $template_to_locate; // Output should be somefile

    NOTE: locate_template expects an array of template names to check for, else it’ll return an empty string, for illustration see below..

    The start of the locate_template function looks like so..

    function locate_template($template_names, $load = false, $require_once = true ) {
    	if ( !is_array($template_names) )
    		return '';

    Thread Starter Yourit

    (@yourit)

    He Mark,

    Thanks for your reaction!
    Yes i do get the template name back this way.
    But all i want is to echo out the $fruit array written in the Templatefile-array.php.

    Template_to_locate doesn’t seem to do the trick right now…

    <?php locate_template(array('Templatefile-array.php' ), false);?>
    <?php echo $fruit; ?>

    How do i fix this?

    You don’t echo arrays, and your “string” is not an array.. (but this is beside the point).

    Stick an echo statement in the Templatefile-array.php file and see if you get output (it will confirm if it’s been included/required correctly).

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Load $variables from other template’ is closed to new replies.