Posted on December 20, 2012
<?php
add_action( 'all_admin_notices', 'testing_testing_testing' );
function testing_testing_testing() {
echo '<div id="message" class="updated">';
get_your_loop_no_trans();
get_your_loop(); // get_your_loop( true ); to not use transient
echo '</div>';
}
// BEFORE TRANSIENTS:
function get_your_loop_no_trans() {
$stuff = array(
array( 'title' => 'thing 1', 'desc' => 'a description' ),
array( 'title' => 'thing a', 'desc' => 'another description' ),
array( 'title' => 'thing 2', 'desc' => 'a third description' ),
array( 'title' => 'thing b', 'desc' => 'a ~fancy~ description' ),
array( 'title' => 'one more thing', 'desc' => 'one more description' ),
); // your object or array to loop through
// Your loop here:
?>
<dl><h3>This is a definition list</h3>
<?php
foreach ( $stuff as $key => $thing ) {
?>
<dt>This is <?php echo $thing['title']; ?><dt>
<dd>Description: <?php echo $thing['desc']; ?><dd>
<?php
}
?>
</dl>
<?php
}
// AFTER TRANSIENTS:
function get_your_loop( $delete = false ) {
if ( $delete == true ) {
// delete the transient if the $delete flag is set to true.
delete_transient( 'your_transient_name' );
$data = false;
} else {
$data = get_transient( 'your_transient_name' );
}
if ( $data === false ) { // if we haven't found a transient, do this
// Will tell you in your source code that the transient is NOT being used
$transuse = "\n<!-- not using transient -->\n";
$stuff = array(
array( 'title' => 'thing 1', 'desc' => 'a ' ),
array( 'title' => 'thing a', 'desc' => 'another ' ),
array( 'title' => 'thing 2', 'desc' => 'a third ' ),
array( 'title' => 'thing b', 'desc' => 'a ~fancy~ ' ),
array( 'title' => 'one more thing', 'desc' => 'one more ' ),
); // your object or array to loop through
// start building our $data variable
$data = '<dl><h3>This is a definition list</h3>';
// Your loop here:
foreach ( $stuff as $key => $thing ) {
// continue building our $data variable
$data .= '<dt>This is '. $thing['title']. '<dt>'.
'<dd>Description: '. $thing['desc']. '<dd>';
}
// This wraps up our $data variable
$data .= '</dl>';
// if our $delete flag isn't set, set our transient!
if ( $delete != true ) set_transient( 'your_transient_name', $data, 86400 ); // time in seconds: 86400 is 24hrs
} else { // This means we HAVE found a transient
// Will tell you in your source code that the transient is being used
$transuse = "\n<!-- using transient -->\n";
}
// Display our $data, (whether it's a transient) or not and wrap it in the $transuse html comments
// in order to tell on the front-end if the transient is being used (view source)
echo $transuse . $data . $transuse;
// if $delete is not set to true, than only the first viewer of the "get_your_loop()" will endure
// the query/loop etc. for subsequent users in the next 24 hours, it will be a lightning fast option grab.
}
Recent Comments