Category List Shortcode
Adds a [category_list] shortcode that prints a linked list of your categories, optionally with post counts and nested subcategories.
Opens WPIDE on
Code
add_shortcode( 'category_list', function ( $atts ) {
$atts = shortcode_atts( array(
'show_count' => 'no',
'hierarchical' => 'yes',
'exclude' => '',
'taxonomy' => 'category',
), $atts, 'category_list' );
$list = wp_list_categories( array(
'title_li' => '',
'echo' => false,
'show_count' => 'yes' === $atts['show_count'],
'hierarchical' => 'yes' === $atts['hierarchical'],
'exclude' => wp_parse_id_list( $atts['exclude'] ),
'taxonomy' => taxonomy_exists( $atts['taxonomy'] ) ? $atts['taxonomy'] : 'category',
) );
return '<ul class="wpide-category-list">' . $list . '</ul>';
} );
Notes
Example: [category_list show_count="yes" exclude="1,7" taxonomy="category"].
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.