Welcome to our new series’ first post! The “WordPress Template Tags” series is intended to bring into spotlight some of WordPress’ less common, but very useful template tags. While the contents can’t be considered a revelation to advanced WP users and developers, it can be of big help to those who take their first steps in the amazing world of online publishing using this famous blogging platform.

But enough chit chat! Here’s our first recommendation:

wp_get_archives()

Have you ever thought of how to display a list of your most recent posts? Or a list of all posts published in the last 10 days? Or 6 months? Or 2 years?
If so, after searching for a solution, you came across a plugin to help you, or used the WordPress’ standard “Recent Posts” widget.

For such a small need, you had to install one more thing, when it could have solved with one tiny line of code using the wp_get_archives() template tag.

What makes this tag so useful is the possibility to customize the result based on a few factors like: type, format, limit, code to be added before & after or post count. Since by default this template tag returns results inside of <li></li> elements, remember to always enclose it in <ul></ul>, like shown below:

Usage & Examples:

1. Display an archive of the last 6 months:

<ul><?php wp_get_archives('type=monthly&limit=6'); ?></ul>

2. Display the latest 10 posts:

<ul><?php wp_get_archives('type=postbypost&limit=10'); ?></ul>

Also, the display mode can be customized so that instead of showing a <ul> / <li> list, the archives could be displayed in a dropdown form element.

More reference: WordPress Codex on wp_get_archives()