How to Easily Display Recent Posts in WordPress
Displaying your latest blog posts is an integral part of any blog design. The most popular method of displaying recent posts is to use the recent posts widget; which is built into the core version of WordPress. All you have to do is drag the widget from your available widget list to an active widget area.
The recent posts widget is basic. There are no options beyond defining how many posts are listed and whether the date of each post is displayed. An alternative method is to use the wp_get_archives function. The function can be used to display a list of date based archives.
In this tutorial, we will show you how you can use the wp_get_archives function to display recent posts on any part of your WordPress website.
How to Display Recent Posts in WordPress Using WP Get Archives
The wp_get_archives function can be used in any WordPress template. It can be called without any parameters.
<?php wp_get_archives(); ?>
The default function will list all monthly archives in descending order in the <li> format. This would provide a list of recent posts like this:
Eight parameters are available for use. These can be used to control exactly what posts are displayed on your list.
Let us look closer at what you can do with each one.
- type (string) – Four date based list types are available. The other two options lists all posts. postbypost lists all posts by post date and alpha lists posts by post title.
- monthly (default)
- limit (integer) – The number of archive links to list. The function will display all lists unless advised otherwise.
- format (string) – The format of your list items. The default is html.
- html – Wraps posts inside HTML list items (<li>).
- option – List posts in a drop down list using <option> tags.
- link – Wraps posts inside <link> tags.
- custom – Allows you to customise what is placed before and after posts.
- before (string) – The text or HTML that is used before the post link when custom has been chosen as the format.
- after (string) – The text or HTML that is used after the post link when custom has been chosen as the format.
- show_post_count (boolean) – Allows you to display the number of posts in date based post lists such as yearly and monthly.
- 1 (True)
- 0 (False)
- echo (boolean) – If you set echo to false, the output will be returned and not displayed on your website. It is not a parameter you will use too often as it already defaults to true if it is not used.
- 1 (True)
- 0 (False)
- order (string) – The order in which post items are displayed. The default order is descending.
We showed you previously how a list of monthly archives can be displayed by simply calling the function without any parameters i.e. wp_get_archives(). The same output can be achieved using all parameters.
<?php wp_get_archives = array( 'type' => 'monthly', 'limit' => '', 'format' => 'html', 'before' => '', 'after' => '', 'show_post_count' => false, 'echo' => 1, 'order' => 'DESC' ); ?>
As you can see, three of the above parameters are blank. In practice, you would simply omit these parameters rather than list them.
Examples of WP Get Archives
The wp_get_archives function can be used in a lot of interesting ways. It is not only for displaying recent blog posts in your sidebar or footer. You can also use the function to create archive pages and sitemaps.
Let us look at some examples of how the function can be used. This is the best way to illustrate how easy it is to display post archives using wp_get_archives.
The code below will list your ten latest posts in descending date order.
<?php wp_get_archives( array( 'type' => 'postbypost', 'limit' => 10 ) ); ?>
The last six months of monthly archives can be displayed using the code below. Note that date archives will always display the full number of posts required. So if you published posts in every month except March; monthly archive links would be displayed for January, February, April, May, June and July.
<?php wp_get_archives( array( 'type' => 'monthly', 'limit' => 6 ) ); ?>
The code below displays the last twelves of weekly archives. It also denotes the number of posts every week. As noted previously, twelve full weeks of weekly archives will always be displayed, therefore weeks without posts will not be listed.
<?php wp_get_archives( array( 'type' => 'weekly', 'limit' => 12, 'show_post_count' => 1 ) ); ?>
The alpha type can be used to displayed all of your blog posts. This is useful for archives and sitemaps.
<?php wp_get_archives('type=alpha'); ?>
The custom format allows you to define what is used before and after post links. A simple vertical bar can be used to divide links.
<?php wp_get_archives( array( 'type' => 'postbypost', 'limit' => 5, 'format' => 'custom', 'after' => ' | ' ) ); ?>
The option format can be used to display a drop down menu for monthly archives.
<select name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;"> <option value=""><?php echo esc_attr( __( 'Select Month' ) ); ?></option> <?php wp_get_archives( array( 'type' => 'monthly', 'format' => 'option', 'show_post_count' => 1 ) ); ?> </select>
We encourage you to try the wp_get_archives function out by yourself. You will soon realise how useful the function is.