Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* add this to your template where you want to display latest posts
- ==========================================
- <div id="latestnews" class="homewidget">
- <h3 class="widgettitle">Latest News</h3>
- <ul>
- <?php
- // Get any existing copy of our transient data
- if ( false === ( $recent_posts = get_transient( 'recent_posts' ) ) ) {
- // It wasn't there, so regenerate the data and save the transient
- $args = array( 'numberposts' => '5',
- 'post_status' => 'publish' );
- $recent_posts = wp_get_recent_posts( $args , FALSE);
- set_transient( 'recent_posts', $recent_posts );
- }
- //store the original post data
- $tmp_post = $post;
- foreach( $recent_posts as $post ) :
- setup_postdata($post);
- echo '<li><h4><a href="' . get_permalink() . '" title="Permalink to '.get_the_title().'" >' . get_the_post_thumbnail(get_the_id(),array(60,60)). get_the_title().'</a></h4></li> ';
- endforeach;
- //restore original $post data
- $post = $tmp_post;
- ?>
- </ul>
- </div>
- /* add this to your functions.php
- ==========================================
- // Create a simple function to delete our recent posts transient
- function delete_recent_posts_transient() {
- delete_transient( 'recent_posts' );
- }
- // Add the function to the save_post hook so it runs when posts are saved
- add_action( 'save_post', 'delete_recent_posts_transient' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement