Customizable Archives Shortcode

You can use the following WordPress shortcode to output custom archives (monthly, yearly, etc.) on posts, pages, HTML Widgets, Thesis Text Boxes, or any other content area that can process shortcodes.

Note: These instructions assume you are using a PHP customization class to organize your custom code.

public function __construct() {
	add_action('template_redirect', array($this, 'front_end')); // Shown for context
	/* Hooks and filters that need to fire early go here */
	add_shortcode('custom_archives', array($this, 'archives'));
}

public function archives($args = array()) {
	if (is_array($args))
		extract($args);
	$depth = !empty($depth) ? $depth : 0;
	$tab = str_repeat("\t", $depth);
	return
		"$tab<ul>\n".
		wp_get_archives(array(
			'echo' => false,
			'post_type' => trim(!empty($post_type) ? $post_type : 'post'),
			'show_post_count' => !empty($count) ? true : false,
			'type' => trim(!empty($type) ? $type : 'monthly'))).
		"$tab</ul>";
}

With your custom code in place, you can now reference this shortcode in a content area:

[custom_archives]

Default output is simply a monthly archive, but you can change this by including a type parameter that can have the following values:

  • yearly
  • monthly (default)
  • daily
  • weekly
  • postbypost — ordered by post date
  • alpha — ordered by post title
[custom_archives type="yearly"]

To include a post count next to each archive link, include a count parameter:

[custom_archives count=1]

To output a custom post type archive only, specify a post_type parameter (the available custom post types will differ depending on your WordPress installation):

[custom_archives post_type="page"]

For OCD HTML source indentation, supply a depth parameter:

[custom_archives depth=5]

And finally, you can combine parameters:

[custom_archives type="yearly" count=1 depth=5]