How toHow to hide No categories if there are no subcategories

Someone is using the put_cat_icons tag in order to display his categories and subcategories in archive.php. This presents a sizeable icon for each subcategory of the currently selected category. How to do this :

$cat = get_cat_ID( single_cat_title("", false) );
put_cat_icons( wp_list_categories('hide_empty=1&orderby=name&echo=0&depth=1&title_li=&child_of='.get_cat_ID( single_cat_title("", false) )));

The problem is that when there is no subcategory, “No categories” is displayed. So to hide this, we’ll use another filter this time, named wp_list_categories. Paste the following code in your functions.php file in your theme.

function bm_dont_display_it($content) {
  if (!empty($content)) {
    $content = str_ireplace('<li>' .__( "No categories" ). '</li>', "", $content);
  }
  return $content;
}
add_filter('wp_list_categories','bm_dont_display_it');

25 thoughts on “How to hide No categories if there are no subcategories

  1. Hi,

    I’ve put this at the end of my functions.php and nothing. Then I had a brainwave and moved it to the front of the file, a few functions down from the top of the page – worked perfectly. Thanks so much for this fix, appreciate it!!

  2. thanks, i used archives.php first by mistake then re-read and saw it supposed to be functions.php

    really helped a lot at 2.30am when ive exausted all options. appreciate it a lot.

  3. *Sigh*
    Was going absolutely nuts trying to figure this one out. You just saved me countless sleepless nights…..!
    Much appreciated…

  4. Many thanks to you!!!! I have been up all night on this, all over the web, and in the WP forums. I put this at the beginning of my functions.php file and that pesky “No Categories” disappeared. I will send you a donation!

    Regards,

    Robert

  5. That was awesome! I have been looking for this solution for 2 days and you got it! Thank you so so so much!

  6. After hours of searching, finally came across this perfect solution! Thanks so much. Got rid of the “no categories” perfectly :)

  7. This is a lifesaver, BUT how do I show no title_li as well? For example I named it “Sub Categories” and now when there are none it shows no Subcategories but it still shows the title_li for it. Any idea?

  8. Thank you very very much man, really. Even after 3 years your trick is still a killer trick !

    THX !!!!

  9. I aslo have “no categories” floating near my nav menu. The problem is I don’t know a lick of code and am afraid to touch it. Can you help me with more details?

  10. OMG men you save the little bit of hair i have left !!!!! 3 days I’ve being looking for a solution to that NO CATEGORIES tab… thank you sooooooooooooooo much

  11. After doing this I got: put_cat_icons error message about echo 0 in wp_list_categories.

    I updated the category_icons to not output ‘put_cat_icons : your list is empty ! Don’t forget to add echo=0 in the parameters of wp_list_categories().’

  12. Well — this was an awful recommendation. I copied and dropped the code in as directed – and now I have a “parse error”, meaning I cannot even access my site or WordPress admin page.

    Obviously the code above is either incomplete or no longer applicable.

    MB

  13. Thank you so much! I’ve been looking for a way to hide all references to comments on this template I’m using and nothing. Deleted all posts and categories and got “No categories” as an eye-sore. Now I can advance with this website. Blessings!

  14. I was looking for a way to solve this forever on my little site, slapped my forehead when a quick ctrl+c ctrl+v fixed everything. Thank you!