有一个简单的函数可以解决变量问题。它和Wordpress在get_template_part()
作用只需复制并粘贴到function.php
function getTemplatePart($slug = null, $name = null, array $params = array()) {
global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
do_action("get_template_part_{$slug}", $slug, $name);
$templates = array();
if (isset($name))
$templates[] = "{$slug}-{$name}.php";
$templates[] = "{$slug}.php";
$_template_file = locate_template($templates, false, false);
if (is_array($wp_query->query_vars)) {
extract($wp_query->query_vars, EXTR_SKIP);
}
extract($params, EXTR_SKIP);
require($_template_file);
}
模板中的用法示例
$params = array(
\'utm_source\' => \'footer\'
);
while ($posts->have_posts()) {
$posts->the_post();
getTemplatePart(\'content\', \'heighlight\', $params);
}
在
content-heighlight.php
是名为的可访问变量
$utm_source
和价值
footer