70

In short, all I need is to make my WordPress do this

$var = get_template_part( 'loop', 'index' ); 

but, get_template_part() does not return HTML, it prints it.
I need this HTML stored in $var - do you have any ideas how to do it?

1
  • would you print it later? I mean the $var. Commented Dec 14, 2014 at 22:32

6 Answers 6

116

This isn't what get_template_part was for, get_template_part essentially behaves like PHP's require function. Justin Tadlock writes a lot more about this here and also talks about a Wordpress function that might be more useful to you - locate_template.

Alternatively, if you did want to hack this functionality using get_template_part, you could use template buffering:

function load_template_part($template_name, $part_name=null) {
    ob_start();
    get_template_part($template_name, $part_name);
    $var = ob_get_contents();
    ob_end_clean();
    return $var;
}
Sign up to request clarification or add additional context in comments.

8 Comments

when i put this in my theme i only get bacon and not the file's contents.... : echo "bacon " . load_template_part('registration-form.php');
I'm not sure where Atomicus says that they want the literal unprocessed PHP, get_template_part acts as an include rather than a file-content-getter.
should comment back to say that you're right this totally works, i was feeding the wrong info to the function (adding .php). <facepalm> though i would set $part_name=NULL by default so you can just called load_template_part('content-aside')
This is the reason why wordpress sucks, very ugly solution and very error sensitive.
@Simon Scarfe: Yes it really sucks because of the old separated header, content and footer structure and you cannot change it without hacking wordpress. I need to change something in the header based on the content but it is impossible without hacking the wordpress template system. Header is generated before content. locate_template doesn't make any sense either because it is an include. Why using the overhead of a wordpress function to include something. Yes there are ways to change the header based on some content but with overhead or very ugly.
|
17

I'm not loving Output Buffering, though +1 for even thinking of that as an option!

I think Helga was on to something, but you need to still respect the child_themes and the theme path, so use locate_template() instead (also as Simon suggested).

This works nicely, and can even be used inside a filter or (in my case) shortcode function (I wanted my shortcode to output the content within a template-style file, to separate the display layer from the logic layer).

return file_get_contents(locate_template("template-file-name.php")); // don't forget the .php!

2 Comments

I tried that, but that didnt execute the template page, is there a way to execute it and return the output string?
You need to use output buffering in that case as per @SimonScarfe's answer.
2

what about?

$file = file_get_contents(STYLESHEETPATH . '/template-part.php'); 
return $file;

i'm sure there is a better way, but that seems to work for me.

Comments

2

If your goal is to create a shortcode with the HTML return, the example below works for me:

function funcao_produtos_filtro_ead() { 

    $html = "";

    ob_start();
    
    // LOOP DE PRODUTOS
    $args = array(
        'post_type' => 'product',
        'posts_per_page' => '-1'
    );

    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();

            wc_get_template_part( 'content', 'product' );

        endwhile;
    }

    wp_reset_postdata();

    return '<div class="woocommerce">' . ob_get_clean() . '</div>';
    
  }

  add_shortcode('produtos_filtro_ead', 'funcao_produtos_filtro_ead');

1 Comment

Thank you! This is exactly what I was looking for :)
2

This worked for me

ob_start(); 
get_template_part( 'loop', 'index' );
$var = ob_get_clean();

Comments

0

I know I'm late to the party, but what I do that doesn't require output buffering is to just put it in a function, save function in functions.php, and save what it returns to a variable.

function get_loop_index() {
  $loop = "<div class='loop'>
            <a class='index' href='foo.bar'></a>
           </div>";
  return $loop
}

$loop_index = get_loop_index();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.