Created
January 4, 2018 08:15
-
-
Save tlehtimaki/7b24a7d921f616c7ffef93164447bfef to your computer and use it in GitHub Desktop.
Pass variables to template in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public static function get_template($template, $variables = array()) { | |
// Pass variables as a key-value array: array('varname' => 'varvalue') | |
// and conditionally check for them in templates like this: | |
// $varname = !empty($varname) ? $varname : NULL | |
foreach ($variables as $key => $value) { | |
// Create variables from parameters | |
${$key} = $value; | |
} | |
require(locate_template($template)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment