How to display recent posts as thumbnails in sidebar in WordPress 3.0+

How to display recent posts as thumbnails in sidebar in WordPress 3.0+

Read this article to learn how to use the new thumbnail feature from latest WordPress versions, to call your recent posts in sidebar.

wordpress-recent-th

This solution was updated for WordPress 3.0+

Starting with WordPress 2.9 is no longer need to use custom fields and hacks to have thumbnails in your blog articles or even on widgets area, sidebars, footers etc. For instance, to make a thumbnails area for random latest 12 recent posts, use the following code:

	<?php

		$my_query = new WP_Query('showposts=12&amp;amp;amp;orderby=rand');
		if( $my_query->have_posts() ) {

		while ($my_query->have_posts()) : $my_query->the_post(); 

	?>

	<?php

		$attachments = get_posts( array(
		'post_type' => 'attachment',
		'number_posts' => 1,
		'post_status' => null,
		'post_parent' => $my_query->post->ID,
		) );
		if ($attachments) {

	?>

	<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">

	<?php

		$thumbnail_id = $attachments[0]->ID;
		echo wp_get_attachment_image( $thumbnail_id );
		}
		endwhile;
		}
		wp_reset_query();

	?>

</a>

You need to keep in mind the thumbnails will have the sizes from your function.php call action settings, and that will not look ok in your sidebar. You can use a CSS style to make them to look how you wish. Example:

.attachment-thumbnail {
height:50px;
width:50px;
padding:5px;
background:#fff;
margin:5px 5px 0 0;
}

Become expert in web designing with testking 642-456 online course. Get testking 642-661 photoshop tutorials and testking 646-230 design guides to learn how to create inspiring designs for your web page.

Network News

Feedbox
Published on: April 26th in Tutorials, Wordpress .
Post tags: , , , , , , , , , , ,

22 Comments

  • kuta kharisma
    April 28, 2010
  • Drew Taylor
    May 3, 2010
  • John
    May 20, 2010
  • DldBox
    May 24, 2010
  • Thai
    October 14, 2010
  • Thai
    October 16, 2010
    • reADactor
      October 17, 2010
  • Jagan Mangat
    February 13, 2011
  • iRenew
    March 31, 2011
  • Hundeting
    April 6, 2011
  • Olive
    April 18, 2011
  • Geekyard
    April 20, 2011
  • batman legos
    May 4, 2011
  • Edille Rosario
    May 5, 2011
  • evan
    May 20, 2011
  • James
    May 24, 2011
  • Karl
    May 26, 2011
  • car
    September 13, 2011
  • muuver
    September 22, 2011
  • Arindam Chakraborty
    September 26, 2011
  • Adler D.
    January 31, 2012

Leave A Comment.