Created
December 12, 2012 02:40
-
-
Save aprakasa/4264393 to your computer and use it in GitHub Desktop.
automatically echo to the screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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