Show Category Post Count with a Shortcode in WordPress

Were You Trying to Show Category Post Count with a Shortcode?

Here is the answer. WordPress does not include the function to Show Category Post Count with a Shortcode by default. Here is a quick php guide to do it. Read through. You can praise me later:)

WordPress is an addiction

You know that as well as I do. It is addictive simply because the unique flexibility it offers. Avenues to explore are unlimited. Problem is more functions we have, more we want.It never stops.You agree?

Recently I started building a website for Sri Lankan song lyrics and chords.Here I have organised the artists into categories and I wanted to show the number of songs I have posted by individual artist, i.e I wanted to show category post count with a Shortcode.

Why shortchode?

It is really easy and flexible when you do it with a shortcode. You can show the post count in a post, page or in a widget.

What did I do

First I looked for an easy way out. I looked for a plugin. Found this one.
Count Posts in a Category, Tag, or Custom Taxonomy
It looked like it was working but it always returned the count as ( 0 ). So I got fed up.

Then I dig harder and found this code in Stack Exchange posted by Ahmed Mahdi. Thank you mate.

// Add Shortcode to show posts count inside a category
function category_post_count( $atts ) {

$atts = shortcode_atts( array(
'category' => null
), $atts );

// get the category by slug.
$term = get_term_by( 'slug', $atts['category'], 'category');

return ( isset( $term->count ) ) ? $term->count : 0;
}
add_shortcode( 'category_post_count', 'category_post_count' );

How to implement

Simply cut and paste this code into your functions.php file. If you have a child theme even better.

How to Use it

Here is the short code for it.

[category_post_count category="category_slug_or_name"]

Just cut and paste it in a post, page or widget. Remember to replace “categeory_slug_name” with the real slug name if your category. For an example, if your slug name is old-cars the shortcode should look like

[category_post_count category="old-cars"]

Got it?

Here is this code in action

Show Category Post Count With A Shortcode, easypressit

You can also use the category name instead the slug all you have to do is change the line

$term = get_term_by( 'slug', $atts['category'], 'category');

to

$term = get_term_by( 'name', $atts['category'], 'category');

You can also use this for other taxonomies like tags. Change the taxonomy slug category to tags.

Now You Know How to Show Category Post Count with a Shortcode

But one last thought….
I expect that you have some experience with you functions.php. If you don’t do it right….If you get the horrible white screen…don’t hold me for it. I am just trying to help you by showing exactly what I did.

You know any other ways or plugins?…Just share with us please

Comment 1

  1. Brian November 14, 2017

Leave a Reply

Sorry, is this not what you wanted?

Just tell me what you are looking for !

I will mail them to you, quick as a flash.

Thank you!

Welcome to Easypress it family

Hey ! Glad you are here.

I love sharing these with you all.
Get more free stuff like this even before they are published.
Come join the rest of my buddies.