Displaying archive links in WordPress using wp_get_archives

The wp_get_archives function in WordPress retrieves a monthly archive of posts. It can be useful for creating a list of past posts organized by month, allowing users to easily navigate through older content on a website.

Parameters accepted by the wp_get_archives function

  • $args (string array, optional): This parameter is optional and its default value is an empty string. It is used to specify the default archive links arguments.

Value returned by the wp_get_archives function

The function returns either void or a string. If the 'echo' argument is set to true, the function returns void. If 'echo' is set to false, the function returns the archive links as a string.

Examples

How to display monthly archives in a list

Use the wp_get_archives function to display a list of monthly archives in WordPress.

<?php
 $args = array(
 'type' => 'monthly',
 'format' => 'html',
 );
 echo wp_get_archives($args);
?>

How to display yearly archives as a dropdown

Use the wp_get_archives function to display a dropdown of yearly archives in WordPress.

<?php
 $args = array(
 'type' => 'yearly',
 'format' => 'option',
 );
 echo wp_get_archives($args);
?>

How to display post archives by post type

Use the wp_get_archives function to display archives of a specific post type in WordPress.

<?php
 $args = array(
 'post_type' => 'your_post_type',
 );
 echo wp_get_archives($args);
?>

Conclusion

In conclusion, the wp_get_archives function is a valuable tool for WordPress developers and website administrators. It provides a simple way to display archives of posts based on various parameters such as post type, date, and format. By utilizing this function, users can easily customize the display of their archives to fit their specific needs. Whether it’s creating a monthly archive list or a dropdown menu of yearly archives, wp_get_archives offers flexibility and ease of use. With its extensive documentation and support within the WordPress community, this function is a reliable choice for managing and displaying archives on a WordPress website.

Related WordPress Functions