Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Display a Category only if it has Posts in WordPress

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Display a Category only if it has Posts in WordPress

This snippet that we are sharing in this article is helpful in very custom designs. By default you can use wp_list_categories function to display categories, and it only displays categories if it has posts. Sometimes when you are customizing WordPress, you might need to use it this way. When we were working on a client’s project, we found a need for this snippet, therefore we are sharing it for anyone else who can use it.

<?php if (get_category('17')->category_count > 0) echo get_category('17')->cat_name; ?> 

In the method above we are specifying the category ID for very specific category if you want to check, but you can do this with all categories also. Just paste the snippet below where you want it.

<?php foreach (get_categories() as $category){
if ($category->count > 0){
echo $category->cat_name;
} ?>

Now how would you use it? Well sometimes you have a category with a specific name, but you want to display the link with a different anchor text, and you only want to display it if it has posts, this way can be handy. So for instance in your navigation menu, you can enter something like this:

<?php if (get_category('17')->category_count > 0) echo "<a href=\"".get_bloginfo('home')."/category/news/\">Blog</a>"; ?>

This will check if category 17 has any posts, if it does, then it will display the navigation menu item called Blog, otherwise it would not.

It’s very simple and easy, but for those new developers it can be helpful.

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. MIke Dutton says:

    I wonder if it possible to adapt this for Taxonomy terms. So only display something if a particular Taxonomy term has posts.

  2. randy says:

    OMG so helpful for running and if statement to see if there are posts in a category, regular post type or custom post type. Many thanks!

  3. John Hartley says:

    Easiest example I found anywhere…saved me some heartache, so thanks!

  4. michelle says:

    really useful, thanks. I have placed a link back to this on my own blog so that I can share it with others :)

  5. Luke Jones says:

    Excuse me if I’m wrong but doesn’t it do this already in the most recent versions?

    • Editorial Staff says:

      Read the first two lines of the post. We already mention that using the wp-list-cats, yes it does, but if you are doing some more customization and need it hard coded without calling a function, then this is the way you will go.

  6. Enk. says:

    It seems interesting but I seriously didn’t understand it. When we can name/rename a category from news to Blog. Whats the thing else than showing only if it has posts? *confused :s

    • Editorial Staff says:

      This is only for very customized settings, but it is still good to know for those who does not. Yes you can rename a category, but how many times. If you have 7 custom page templates where you want to call one category 7 different names, then how would you go about it. For Sales / Splash pages, this will help.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.