Nested Categories

Topics: Controls
Feb 2, 2012 at 10:26 PM
Edited Feb 2, 2012 at 11:26 PM

Hi Folks, 

Just upgraded the nested categories widget on my site.

All sub categories are now nested within "All Categories" which can be viewed by date or popularity.

Uncategorized posts can also be viewed, but only shows in the list if there are actually any uncategorized posts.

A link to view the post archive is now available from within the widget.

You can view it here

Just looking for some feedback, feature suggestions etc. and whether or not to upload this version to the gallery.

Feb 3, 2012 at 12:23 AM


Looks good! Small point, "Articles" has 3 articles in it, yet the total of Apple, Tables and Windows Phone is 5...


Feb 3, 2012 at 12:52 AM

Thanks Dave, 

Fair point, wasn't quite sure how to handle that. 

The parent category "Articles" has 3 distinct posts within it, with each sub category having separate distinct posts, 3, 1 and 1.

Total posts for all "Articles" being 8, trouble is in showing exactly what's in each category.

If you show everything within each top category, how do you represent the distinct posts in each top category, although I think WordPress might be doing it like that.

I'm not sure which way is better or what's most practical given that the underlying structure is flat.


Giving it more thought.

Feb 3, 2012 at 1:06 AM

Or remove the counts. :-)

Aggregating subcategory counts is tricky.  I have an app that maintains accurate category item counts, but requires SQL triggers and a good bit of TLC.  It's a beautiful thing though.

Good luck,

Feb 3, 2012 at 1:22 AM

Nice one :-) 

Funny you should mention it though, it's one of the control options.

I did shirk a little at the thought of aggregating the sub cat counts, or cats for that matter.

The app you mention, is that a Sueetie thing?

Anyways, barley injection time - 3 or 4 ought to do it.


Feb 3, 2012 at 11:44 AM


It's the Marketplace app at  It started out as an old Classifieds ASPNET Quick Starter app, which is where the categorization counts, subcategory storage logic, DB triggers and so forth came from.  Good news and bad there.  You can probably still grab the original classifieds app source if you want to see the details.


Feb 3, 2012 at 8:09 PM


Nicely done, something like that would be ideal.

The problem with BE categories as you know, is that although there is linkage through the parent child arrangement, there is no inherent support for grouping.

I was thinking about trying to aggregate categories to represent the groupings, but then you would have to consider aggregating the RSS feeds to match as well, amongst other things - as you say, a lot of work which goes way beyond a simple add on.

I suppose it's a question of how to represent the parent child relationship without inferring any kind of grouping operations.

Where there's a way there's a how - as they say.

Thanks for the feedback, I'll keep plugging.


Feb 5, 2012 at 6:10 PM
Edited Feb 6, 2012 at 3:00 PM

Hi Folks, 

Still plugging away.  

Been testing something locally and it works well, it's based on the notion that a parent category should include all posts for the parent category as well as all posts in any child categories. 

For example, related categories: 

General - having 2 posts of it's own, Specific - having 1 post of it's own and Very Specific - having 1 post of it's own displays as follows: 

General Category (4)

            Specific Category (2)

                        Very Specific Category (1) 

So if you click on General Category it shows 4 posts: 2 General, 1 Specific and 1 Very Specific. Clicking on Specific shows it's own Specific post and the single post in Very Specific. 

Under the bonnet this works by looking at each post's category list and if any category has a parent, then it gets added to the post category list (the routine being triggered by post or category changes). Any parent child relationship is then rendered as nested lists to represent the hierarchy and can be styled to suit. 

However, this has implications for: 

1. Post data integrity

2. Category links in post headers

3. Archive listings 

Changes made to the post category lists are not automatically reversible and you might not be comfortable with a control changing your post data.

Category links in post headers would by default, show links from the full parent child trail, this could be filtered but has introduced a dependency: code would have to be added to the theme's post view to handle this. Likewise for the archive page. 

With the standard category control or widget if you click on a category it shows the posts for that specific category only, which is great if there is no parent child relationship. On checking the URL you see something like "/category/Articles.aspx" The mechanics of how this happens probably holds the key to a nice solution, but I can't locate this in code - anybody got any hints, suggestions?


Palm to head, default.aspx for displaying cat groups. Checking out syndication options for feed buttons, would much prefer to avoid adding to post category lists.

Feb 5, 2012 at 10:30 PM

The latest version is now live, if anyone wants to check it out.

I still have to figure out what to do with the archive page and the category links in the post headers, but it lets you see how things work.

Any suggestions or ideas are welcome.

Feb 7, 2012 at 2:01 PM

The solution to displaying nested categories by group actually turned out to be fairly simple. 

The display of categories is handled by default.aspx.cs and feed list items are generated in SyndicationHandler.cs(core file). 

For anybody that might be interested, or wants the additional functionality, these are the steps. 

Create a new method in Post.cs (core file) that resembles GetPostsByCategory, like so: 

public static List<Post> GetPostsByCategoryTree(Guid categoryId)

which returns all posts in that category and any child categories. 

Then in SyndicationHandler.cs you simply change the method in GenerateItemList from

GetPostsByCategory to GetPostsByCategoryTree. 

Similarly, for default.aspx.cs in DisplayCategories() 

PostList1.Posts = Post.GetPostsByCategoryTree(categoryId).ConvertAll(new Converter<Post, IPublishable>(delegate(Post p) { return p as IPublishable; })); 

The good thing about it being that it works the same for flat or nested categories. 

I'm going to play around with it a little more, but so far, so good.