Advertisement
helgatheviki

WordPress Show Latest Posts without Plugin

Jan 30th, 2012
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.38 KB | None | 0 0
  1. /* add this to your template where you want to display latest posts
  2. ==========================================
  3. <div id="latestnews" class="homewidget">
  4.     <h3 class="widgettitle">Latest News</h3>
  5.         <ul>
  6.             <?php
  7.             // Get any existing copy of our transient data
  8.             if ( false === ( $recent_posts = get_transient( 'recent_posts' ) ) ) {
  9.                 // It wasn't there, so regenerate the data and save the transient
  10.                 $args = array( 'numberposts' => '5',
  11.                         'post_status' => 'publish' );
  12.                 $recent_posts = wp_get_recent_posts( $args , FALSE);
  13.                          
  14.                  set_transient( 'recent_posts', $recent_posts );
  15.              }
  16.  
  17.             //store the original post data
  18.             $tmp_post = $post;
  19.  
  20.             foreach( $recent_posts as $post ) :
  21.                 setup_postdata($post);
  22.                 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> ';
  23.             endforeach;
  24.            
  25.             //restore original $post data
  26.             $post = $tmp_post;
  27.             ?>
  28.     </ul>
  29. </div>
  30.  
  31.  
  32.  
  33. /* add this to your functions.php
  34. ==========================================
  35. // Create a simple function to delete our recent posts transient
  36. function delete_recent_posts_transient() {
  37.      delete_transient( 'recent_posts' );
  38. }
  39. // Add the function to the save_post hook so it runs when posts are saved
  40. add_action( 'save_post', 'delete_recent_posts_transient' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement