/**
* This creates the [my_cat_list] shortcode.
*
* The shortcode will display a list of categories.
*
* @link https://wpbitz.com/?post_type=code-snippets&p=118&preview=true
* @see https://wordpress.stackexchange.com/questions/182812/create-a-category-list-page/182813#182813
*/
add_shortcode( 'my_cat_list', 'my_list_categories_shortcode' );
function my_list_categories_shortcode( $atts, $content, $tag ) {
/**
* List of possible values for arguments.
*
* Shortcode attributes are always evaluated as strings. Make sure that
* comma separated values are passed as strings, not arrays. For booleans,
* you can pass string "true"|"false" values or 0|1 values and the function
* will adjust it to a true boolean.
*
* The "echo" argument is always set as false. Shortcodes should never
* echo their output - only return it.
*
* For more information on possible arguments, see the following:
* @see https://developer.wordpress.org/reference/functions/wp_list_categories/
*
* 'exclude' (string) Comma/space-separated string of term IDs to exclude. If $hierarchical is true, descendants of $exclude terms will also be excluded; see $exclude_tree. See get_terms().
* 'exclude_tree' (string) Comma/space-separated string of term IDs to exclude, along with their descendants. See get_terms().
* 'feed' (string) Text to use for the feed link. Default 'Feed for all posts filed under [cat name]'.
* 'feed_image' (string) URL of an image to use for the feed link.
* 'feed_type' (string) Feed type. Used to build feed link. See get_term_feed_link(). Default empty string (default feed).
* 'separator' (string) Separator between links. Default <br />.
* 'show_option_all' (string) Text to display for showing all categories.
* 'show_option_none' (string) Text to display for the 'no categories' option. Default 'No categories'.
* 'style' (string) The style used to display the categories list. If 'list', categories will be output as an unordered list. If left empty or another value, categories will be output separated by <br> tags. Default 'list'.
* 'title_li' (string) Text to use for the list title <li> element. Pass an empty string to disable. Default 'Categories'.
*
* 'current_category' (int) ID of category, or comma separated IDs of categories, that should get the 'current-cat' class. Default 0.
* 'depth' (int) Category depth. Used for tab indentation. Default 0.
*
* 'show_count' (bool|int) Whether to include post counts. Accepts 0, 1, or their bool equivalents. Default 0.
* 'use_desc_for_title' (bool|int) Whether to use the category description as the title attribute. Accepts 0, 1, or their bool equivalents. Default 1.
* 'hide_title_if_empty'(bool) Whether to hide the $title_li element if there are no terms in the list. Default false (title will always be shown).
*/
$bool_fix = array( 'show_count', 'use_desc_for_title', 'hide_title_if_empty' );
if ( is_array( $atts ) ) {
foreach ( $atts as $key => $value ) {
if ( in_array( $key, $bool_fix ) ) {
$atts[ $key ] = ( 'true' == $value || 1 == $value ) ? true : false;
}
$args[ $key ] = $atts[ $key ];
}
}
// 'echo' must always be false.
$args['echo'] = false;
return wp_list_categories( $args );
}
Shortcode to display list of categories
This post brought to you by RocketGeek, ButlerBlog, and the following: