Skip to content

Instantly share code, notes, and snippets.

@aprakasa
Created December 12, 2012 02:40
Show Gist options
  • Save aprakasa/4264393 to your computer and use it in GitHub Desktop.
Save aprakasa/4264393 to your computer and use it in GitHub Desktop.
automatically echo to the screen
<?php
/**
* automatically echo to the screen using transient
*
* @link http://speckyboy.com/2011/12/14/website-speed-part-3-caching-wordpress/
* /
$loop_output = get_transient( 'loopOutput' );
if ( false === $loop_output ) {
// Show the last 100 published posts.
$query = array('post_per_page' => 100,
'post_status' => 'publish' ) ;
// run the query
$loop = new WP_Query($query);
// start the output buffer to save contents of loop
ob_start();
// do normal loop stuff
if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post();
// show content or whatever you like
?><h1><?php the_title() ?></h1><?php
the_content();
endwhile;endif;
// save the output buffer contents in a variable
$loop_output = ob_get_contents();
// clean the buffer as we will be using the variable from now on
ob_end_clean();
// transient set to last for 1 hour
set_transient('loopOutput', $loop_output, 60*60);
}
// output the new created loop if loop content does not exist.
echo $loop_output;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment