WordPress: How to show categories and archives in your post or page without using plugin?

In a previous post, I show you how to display recent posts in your WordPress post or page without using a plugin. In this post, we continue to write some code to show WordPress categories and archives in post or page.

Display categories and archives in WordPress post or page

To display something dynamically in a post or page, we have to use shortcode. So we will write a function to get list of categories and create a shortcode for that function. We will do the same to show archives. We have two blocks of code below:

Code for displaying categories:

//Category list shortcode
function my_category_list( $atts) { 	
	$args = array(
  		'orderby' => 'name',
  		'style' => 'list',
		'title_li' => '',
		'echo' => 0
	return '<ul>'.wp_list_categories($args).'</ul>';
add_shortcode("category-list", "my_category_list");

Code for displaying archives:

//Archives shortcode
function my_archive_list( $atts) { 
	extract(shortcode_atts(array('type' => 'monthly',), $atts));	
	$args = array(
  		'type' => $type,
  		'format' => 'html',
		'echo' => 0		
	return '<ul>'.wp_get_archives($args).'</ul>';
add_shortcode("archives", "my_archive_list");

You just copy the code above, then paste it at the end of your theme functions file (functions.php) and save it. It’s very simple and easy. Now, you can show a list of categories in your post or page by using the shorcode:


To display blog archives, use the shortcode:


By default, the archives shortcode will show archives by month. If you want to show other archive types, you need to add the type attribute to your shortcode: type=daily for date, type=monthly for month, type=yearly for year. For example, the shortcode for archives by date will look like this:

[archives type=daily]

I hope this post is helpful for you. Good luck!

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on RedditShare on StumbleUponShare on LinkedInPin on Pinterest
Blogging, ,
Make money with LinkBucks
About Nhut Le
Hello! My name is Nhut Le. I'm currently a software developer. You can subscribe to my RSS to get instant updates delivered to your email.