Sub-Category Counting

Arvind raised a good question on the Six Apart Pronet mailing list about counting entries within sub-categories.

The MovableType template tag, <$MTCategoryCount$>, only considers entries in the current category and not in any sub-categories. Arvind wants to count the entries in sub-categories too. So if your current category contains sub-categories, you get the count from those sub-categories as well.

I thought that surely I had seen this somewhere and just missed it, but it turns out that there is no such thing (that I could find). So I created a little plugin to do the trick. This plugin offers a single template tag, <$MTSubCatCount$>, that will do the work Arvind requested. It simply counts the current category, then checks to see if there are any sub-categories for the current category and counts those too.

Unlike the default MT tag, this one will work in an archive category context as well (though other tags may not, so it might not help much). It will also work recursively – say, if you have categories within categories within categories within categories.

There are two available attributes on the tag.

I created the first attribute, restrict, to make the tag behave like the MT default – namely, if you’d like to see only the current category count. If you’re doing this, you probably don’t need to use this plugin, but you can if you’d like. Just set it to 1 (restrict=”1″).

The second tag is used to adjust your counts. Arvind wanted this because he has some “placeholder” entries in his categories that he doesn’t want showing up in the count. So I added it. Use the variance tag, along with a positive or negative number, to adjust the count for each category by the number you specify. For Arvind, he would add variance=”-1″ to the tag, and it will automatically reduce the count by 1 for each category. This accounts for his placeholder entries.

Nothing fancy to this one. Just download the source to your computer, unzip the .pl file, then upload it to your server in the plugins folder underneath your main Movable Type directory. That’s it. Enjoy.


Posted

in

Comments

One response to “Sub-Category Counting”

  1. Chad Everett Avatar

    Sorry, broke that while working on the multiple levels of subacategories. Should be fixed now – use the same link to download.