To display a list of categories on your page by just putting something into the content area, you need a shortcode.
You can create a shortcode by using add_shortcode
. This defines the tag and the function to call when that shortcode is used.
Here’s a basic example that creates a shortcode [my_cat_list]:
/**
* This creates the [my_cat_list] shortcode and calls the
* my_list_categories_shortcode() function.
*/
add_shortcode( 'my_cat_list', 'my_list_categories_shortcode' );
/**
* this function outputs your category list where you
* use the [my_cat_list] shortcode.
*/
function my_list_categories_shortcode() {
$args = array( 'echo'=>false );
return wp_list_categories( $args );
}
Adding this snippet to your theme’s functions.php file will create the shortcode.
Placing the shortcode [my_cat_list] into a post or page will then display a list of categories with links to them.
The example uses wp_list_categories()
in the shortcode function to display a list of categories. The example just relies on the defaults for the function, but there are quite a few options for the way the list is output. See the documentation in the codex for wp_list_categories for a full explanation of all of the defaults and options and what they do.