Saturday, 25 April 2015

Filled Under:

How to Display Recent Posts in WordPress

SOCIALIZE IT →
How to Display Recent Posts in WordPress

Displaying Recent posts often helps your users to visit them easily specially on the sidebar of a single post page. But in some designing processes people want to display recent posts in many different ways. In this post, we will show you various different ways you can display the recent posts in WordPress.

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 pasting the following code in a template file of your choosing for example sidebar.php:

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

You can change the number 10 to the number of posts you like to display.
If your theme support Widgets, then there is an easier option for you. You can simply head over to the widgets page and add Recent Posts widget to your sidebar. That will save you from editing the codes.

Displaying Recent Posts with Summary

Some people like to display recent posts with a title and a short description. There are multiple ways of accomplishing that.
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>
And you make sure that the excerpt is a short description of the post. You must rewrite the excerpt to make it fit the word limit.
Another way to limit the number of characters displayed in the content is by manually stripping out post content during the query to display only a limited number of characters. To do that 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

Some people like to display recent posts with full content if WordPress is being used as a Content Management System (CMS).
<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. This is mostly used to make a page look like your blog page. So you should not need this if you are not running a CMS.
Now you should be able to display Recent Posts in your WordPress blog or a CMS.




0 comments:

Post a Comment

  • About Us
  • Disclaimer
  • Advertise
  • Free Wordpress Video Course
  • Back To Top