Customizable Category List Shortcode

You can use the following WordPress shortcode to output a list of your categories 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_categories', array($this, 'categories'));
}

public function categories($args = array()) {
	if (is_array($args))
		extract($args);
	$depth = !empty($depth) ? $depth : 0;
	$tab = str_repeat("\t", $depth);
	return
		"$tab<ul>\n".
		wp_list_categories(array(
			'echo' => false,
			'exclude' => !empty($exclude) ? trim($exclude) : false,
			'hierarchical' => !empty($hierarchical) ? true : false,
			'show_count' => !empty($count) ? true : false,
			'title_li' => '')).
		"$tab</ul>";
}

With your custom code in place, you can begin to use the shortcode in various content areas within WordPress. Here’s a basic implementation:

[custom_categories]

If you want to display a post count alongside each category, you can add a count parameter:

[custom_categories count=1]

If you’re OCD like me and want to ensure the HTML source indentions are perfect, you can add a depth parameter:

[custom_categories depth=5]

You can combine parameters, too:

[custom_categories count=1 depth=5]

Available Parameters:

  • count โ€” (boolean) show number of posts within a category
  • depth โ€” (string) number of tab indentions for OCD HTML source management
  • exclude โ€” (string) comma-separated string of category IDs to exclude from output
  • hierarchical โ€” (boolean) display categories as a hierarchical list