SHOWING RECENT POSTS in STATIC PAGE

Posted: October 17, 2010 in PHP
Tags: , , ,

Using shortcode to display recent posts in static page. If your theme support the “post-thumbnail” option then you can displayinh the thumbnail to your recent post. Otherwise you can use custom field(‘thumb’) option to display the thumbnail to your post. You can use the_excerpt or the_content function according to you choice. I am sharing the shortcode:

Shrotcode:

[recent_posts numposts=5 excerpt=true]

 

Showing recent posts to your static page

 

Add the following code to your functions.php file

add_shortcode('recent_posts', 'recentpostOnstaticpage');
 function recentpostOnstaticpage($atts){
 global $wp_query;
 //extract page name from the shortcode attributes
 extract(shortcode_atts(array( 'numposts' => '3', 'excerpt' => true), $atts));
 $tmp = $wp_query;
 $wp_query = null;
 $output = '';
 $wp_query = new WP_Query('showposts='.$numposts);
 while(have_posts()): the_post();
 $output .= '<div '. get_post_class() .' id="post-'. $post->ID .'">' . "\n" .
 ' <div>' . "\n" ;
 if(current_theme_supports("post-thumbnails") &&    has_post_thumbnail()):
 $output .= '    <div>
 <a href="'.get_permalink($post->ID).'" rel="bookmark" title="Permanent Link To '. the_title_attribute(false) .'">
 '. get_the_post_thumbnail(null, 'thumbnail') .'
 </a>
 </div>';
 elseif (get_post_meta($post->ID, 'thumb', true)):
 $postimageurl = get_post_meta($post->ID, 'thumb', true);
 $output .= '<div>
 <a href="'.get_permalink($post->ID).'" rel="bookmark" title="Permanent Link To ' . the_title_attribute(false).'">
 <img src="'. $postimageurl .'" alt="Post Pic" width="200" height="200" />
 </a>
 </div>';
 endif;
 $output .= '<div style="width: 345px">';
 $output .= '    <div>
 <div>
 <h2><a href="'. get_permalink($post->ID) .'" rel="bookmark" title="Permanent Link to ' . the_title_attribute(array('echo'=>false)) .'">'. the_title('','',false) .'</a></h2>
 <div>
 <em>
 On '. get_the_time(get_option('date_format')) .',
 in ' . get_the_category_list(', ') .',
 by ' . the_author() .'
 </em>
 </div>
 </div>
 </div>
 <!--/post-title -->';

 if($excerpt):
 $output .= '<div>
 '. get_the_excerpt() .'
 </div>';
 else:
 $output .= '<div>
 '. get_the_content(__('Continue reading &raquo;

')) .'
 </div>';
 endif;
 $output .= ' </div>
 </div>';

 $output .= '<div>
 <div>
 <span>'.get_comments_number(0, 1, '%') .'</span>
 </div>
 <div>
 <a href="<?php the_permalink(); ?>#respond">
 Leave A Response
 </a>
 </div>
 </div>
 </div>';
 endwhile;
 wp_reset_query();
 $wp_query = $tmp;
 $tmp = null;
 return $output;
 }

More details

Advertisements
Comments
  1. Ilja says:

    Thank you for this! This is what I have been looking for!

    I have some questions though, I hope you can help. I am not that good with writing code myself.

    Can I also select a category to get the latest posts from? I use different categories and it would be great if I can put them on different pages.

    If I use the shortcode without excerpt=true I still get an excerpt. How can I get the full post?

    Thanks again 🙂

    • chinmoy29 says:

      Hi Ilga,

      Thanks for comment.

      Can I also select a category to get the latest posts from? I use different categories and it would be great if I can put them on different pages.

      If you want to get a post from specific category then you will provide category ID of that post. For that reason need some changes in my code. Change line no 05 and 09.
      Line 05: extract(shortcode_atts(array( 'numposts' => '3', 'excerpt' => true, 'cat'=> ''), $atts));
      Line 09:
      $qstring = ($cat !="") ? '&cat=' . $cat : "";
      $wp_query = new WP_Query('showposts='. $numposts . $qstring);

      If I use the shortcode without excerpt=true I still get an excerpt. How can I get the full post?

      For this use excerpt=false.

  2. Ilja says:

    Thanks so much for you quick response and your help!

    The category selection works :), but the excerpt=false doesn’t. I still do not get the full post.

    This is what I use:

    [recent_posts cat=5 numposts=1 excerpt=false]

    Any ideas?

    Thanks!

  3. Ilja says:

    I tried something else:
    Page content:
    ____
    static text on page

    [recent_posts cat=5 numposts=2 excerpt=false]
    _____

    This happens:

    It puts the second oldest post at the top of the page, full post,
    then the static page content
    and then the two posts as excerpt.
    Example: http://www.wereldontdekkingen.nl/?page_id=612

    I hope you can help with this.

  4. luay says:

    thank you very much
    how i can veiw the thumbnail ?

  5. luay says:

    thanks chinmoy29.
    can you help me with makeing any image i want view with a slide show like this site
    http://themes.mysitemyway.com/infocus/gallery/showcase/
    thanks again

    • chinmoy29 says:

      use jquery lightbox or thickbox plugin for this. There have lot of wp plugin for this. search on google like wp lightbox plugin or wp thickbox plugin. or you will get much better plugin. Plz search on google.

  6. luay says:

    thank you very much
    can i have you email please ?

  7. Hi,

    I love this code. It adds the posts perfectly except that it’s putting this at the top of the text on the page

    adminadminadminadminadminadminadminadminadminadmin

    Here’s the link http://www.sportlaw.ca/writings/

    Thanks
    Janice

    • chinmoy29 says:

      Janice Arnoldi,

      Please use get_the_author() function instead of the_author() function . Find the_author() function from my code and replace with get_the_author() .

      Thanks
      Chinmoy

      • Worked perfectly. Not sure why because I had commented out the_author() because I didn’t want the author on the post either. I changed it to get_the_author and left if commented out and it worked.

        Thanks
        Janice

  8. vitalix says:

    Thanks! Helped a lot, everything I’ve been looking for!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s