Display WordPress Recent Posts Without Plugins

We know that wordpress offers you so many plugins. We also know that the importance of different plugins for different purpose. We can use these plugins for make work easy but there is some problem when wordpress is updating to new version many plugins become incompatible with new code. One more problem is that when we use so many plugins for small task these plugins slow down your website which effects website rating.


You May Also Like:


Compensate this type of problems we use small piece of code instead of plugins. Here we learn how to display wordpress recent posts with thumbnail without using plugins.

Step#1: Setting up TimThumb PHP Script:

This code would help us generate thumbnails for our recent posts. You can download the script from this link.

Download

 

It was developed by Tim McDaniel and paste it into WordPress theme. i.e. WordPress >> WP-content >> themes >> your theme name >> save it in with this file name ‘timthumb.php.

Step#2: Making it work:

Now paste the following code anywhere in your theme function file to make it work. If you have a widget theme then, you can directly paste it in your sidebar and it will start working.

<div id="wpr-recent-posts">
<?php $postslist = get_posts('numberposts=10&order=DESC'); foreach ($postslist as $post) : setup_postdata($post); ?>

<a href="<?php the_permalink(); ?>"><img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=/<?php
$values = get_post_custom_values("Image"); echo $values[0]; ?>&w=300&h=275&zc=1&q=100"
alt="<?php the_title(); ?>" class="related-thumb"  width="40px" height="40px"  /></a>

<p><a title="Post: <?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?> &raquo;</a></p>

<p><?php the_time('F j, Y') ?> : <?php comments_number('0 comments','1 comment','% comments'); ?></p>

<?php endforeach; ?>
</div><!-- End Recent Posts -->

Step#3: Styling

By default, WordPress themes have built-in styling for recent posts, but here is a simple CSS styling for recent posts with Thumbnails.

#wpr-recent-posts {
width: 269px;
margin: 0 0 20px 0;
padding: 10px 20px 10px 0;
}
#wpr-recent-posts p {
margin: 0 0 20px 0;
font-size: 12px;
}
.related-thumb {
width: 40px;
height: 40px;
border: 3px solid #555;
margin: 3px 6px 15px 3px;
padding: 0;
float: left;
}

Displaying Recent Posts in a List Format

The list format is mostly used in sidebars of WordPress pages. You can display the recent posts by simply adding this short code for example sidebar.php:

<?php wp_get_archives( array( 'type' => 'postbypost', 'limit'=> 5, 'format' => 'html' ) ); ?>

You can change the number 5 to the number of posts you like to display.

If your theme support Widgets, then there is an easier option for you.

Displaying Recent Posts with Summary

Some people like to display recent posts with a title and a short description. By using this code you can display the recent post with little description.

The first way is:

<ul>
<?php $the_query = new WP_Query( 'showposts=5' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<li><?php the_excerpt(__('(more…)')); ?></li>
<?php endwhile;?>
</ul>

Another way to limit the number of characters displayed

Another way to display short description with recent post to use this code.

<ul>
  <?php $the_query = new WP_Query( 'showposts=5' ); ?>

  <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
  <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>

  <li><?php echo substr(strip_tags($post->post_content), 0, 250);?></li>
  <?php endwhile;?>
</ul>

You may change the 250 to set the character limit of your desire.

Displaying Recent Posts with Full Content

By using this code you can display the recent post with full content

<ul>
<?php $the_query = new WP_Query( 'showposts=5' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<li><?php the_content(__('(more…)')); ?></li>
<?php endwhile;?>
</ul>

You may change the number 5 to whatever you like.

 

About The Author

Related Posts

Leave a Reply

Your email address will not be published.