Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

While I've typically used include or require on their own to save long term code maintenance I've started to use get_template_part and locate_template as using built in WordPress stuff is always best.

My question is are you supposed to be able to pass variables through to the results of either get_template_part or locate_template?

<?php
$var = get_option( 'my-custom-option' );

get_template_part( 'custom-template-part' );
?>

In the code above the $var would be printed inside the custom template but the variable doesn't seem to work. Am I missing something or is this expected behaviour?

I've found that they don't pass in the instance above or when using locate_template

<?php
locate_template( 'custom-template-part.php', true );
?>
share|improve this question
add comment

4 Answers

up vote 27 down vote accepted

Like MathSmath wrote, get_template() does not support the re-use of your variables.

But locate_template() infact does no inclusion at all. It just locates a file for inclusion.

So you can make use of include to have this working just like you expect it:

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

$var from your example can be used in the template part then.

A related question with a more technical explanation of the variable scope and get_template(): Form Submitting Error with get_template_part()

share|improve this answer
    
Good call. I didn't notice that locate_template() has a param that lets you optionally call load_template() with the results (which get_template_part does), or just return them. I'm looping back on a current project to update code using this approach...thanks! –  MathSmath Nov 24 '10 at 18:07
    
shortly after I posted here I ended up using the same method. –  curtismchale Nov 24 '10 at 21:22
    
21676 addresses this, but doesn't look like it'll be committed. –  Ian Dunn Feb 19 '13 at 19:05
add comment

I've had trouble with this too (while trying to get a custom query to work with a template part). The short answer is: no, the template part doesn't automatically inherit custom vars the way a regular include does.

Both get_template_part() and locate_template() eventually use the load_template() function to actually load the file (using a require). This function globalizes the following vars:

$posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID

However, no other vars appear to be available from inside the template part. I guess since the actual require is wrapped in a function, the scope changes or something?

Anyhoo, I'd try globalizing any additional vars you need to pass, then calling up those globals from your template part.

share|improve this answer
add comment

Just my two cents for future references, a workaround at least in Wordpress 3.5 is to add the variable to $wp_query->query_vars.

I needed my global _vk_errors inside a template part and just did $wp_query->query_vars['_vk_errors'] = $_vk_errors; before calling get_template_part().

share|improve this answer
add comment

There is my simple function solving variable problem. It's doing same thing as Wordpress do in get_template_part() function. Just copy and paste to 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);
}

Example of usage in template

$params = array(
    'utm_source' => 'footer'
);
while ($posts->have_posts()) {
    $posts->the_post(); 
    getTemplatePart('content', 'heighlight', $params);
}

In the content-heighlight.php is accessible variable with name $utm_source and value footer

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.