WordPress Shortcode to get Total Number of Categories

Here’s a shortcode to get the total number of categories in a post in WordPress.

Shortcode: [total_categories]

Code for the shortcode:

// To get total categories

function sw_total_categories() {
$args = array( 'parent' => 0, 'hide_empty' => 0 );
$categories = get_categories( $args );
return count($categories);
}
add_shortcode('total_categories','sw_total_categories');
PHP

Wherever you want to display the total number of categories, you can simply use the [total_categories] shortcode.

How to add this code to your WordPress site

Leave a Comment