基于@toscho片段,这里有一个真实的例子。假设您不需要直接从模板文件内部访问var。
function wpse_69365_var_storage()
{
static $internal;
if ( \'wp_head\' === current_filter() )
{
if ( /* Condition is met */ )
return $internal = 4; // Set to what you need.
return \'default_value\';
}
return print $internal;
}
// Call in header.php:
add_action( \'wp_head\', \'wpse_69365_var_storage\' );
// Call in footer.php:
add_action( \'wp_footer\', \'wpse_69365_var_storage\' );