EDIT: See below for plugin notes, attached plugin.
In response to mdesign’s Twitter rant this evening, I worked out the code to display any level of subcategories as a nested list. I’m planning on building a plugin around it. Here’s the code to try out. Let me know how it works or if you find it useful.
A few notes:
This is a recursive function (it calls itself til it runs out of nodes to add). The last line of PHP kicks things off with a category ID of 29, which should be replaced by your parent category ID.
The optional show_cat parameter indicates whether to show as root node the category whose ID you passed. By default it’s disabled, and the recursive calls treat it as such, but in the initial call I’ve enabled it by passing 1.
You’ll need to enable PHP in your template if you drop this code in.
REMOVED FORMER PHP SAMPLE CODE. This is now a functional plugin.
Current usage: {exp:recursive_subcategories parent_id="29" show_parent="1"}{category_name}{/exp:recursive_subcategories} Supports category_id, category_name, category_url_tile and category_description tags Supports sort_by (default = cat_order), options = name, id, order Supports sort_direction (default = asc), options = asc, desc Supports backspace Supports style (default = linear), options = linear, nested
Thanks for getting your hands dirty with this tonight. I’m willing to bet I’ll need one thing: the ability to output a comma-delimited list, meaning this plugin would do well with a couple things: a linear output, and possibly a few other things like count, total_results, and backspace.
However, at this point I’d be happy with a linear output. Maybe it could default to nested, with an option for a linear.
But hey, let me download and use this first ahem
I would like to see the plugin be able to be used like the following, with an opening and closing tag, and variables (as here’s a suggestions for the plugin name):
{exp:supersubcats parent_id="29" show_parent="1" style="linear" backspace="1"}
{count}. {category_name},
{/exp:supersubcats}
Great start, though!
I would like to see the plugin be able to be used like the following, with an opening and closing tag, and variables (as here’s a suggestions for the plugin name): … Great start, though!
Thanks!
I was thinking infinitesubcats as a name, as that’s the real distinguishing factor: It can go an infinite number of levels deep.
It does need to be structured with variables (on the to do list), and I’m in full agreement that a linear approach is needed. I prefer to be able to get at my data sans markup as well.
Alongside the traditional cat variables, you would need a “hierarchy level” or “indent level” variable, to know when you were shifting to a sublist of the current category. If you were to generate you own UL, you would need to be able to compare that to the previous indent level to know it was a sublist. I’m curious - How would you denote that in a comma-separated list?
Alongside the traditional cat variables, you would need a “hierarchy level” or “indent level” variable, to know when you were shifting to a sublist of the current category. I’m curious - How would you denote that in a comma-separated list?
In both cases where I’ve needed to do a comma separated list, we didn’t need any way to separate them at all, just list them all out as if they were all on the same level.
EDIT: Apparently I’m only supporting use of ONE tag in output, though it can be any one of the supported tags. Will fix in the next update.
Updated to Beta 1.10, supporting true tag usage.
{exp:recursive_subcategories parent_id="29" show_parent="1"}{category_name}{/exp:recursive_subcategories}
Current version: Supports category_id, category_name, category_url_tile and category_description tags Supports sort_by (default = cat_order), options = name, id, order Supports sort_direction (default = asc), options = asc, desc Supports backspace Supports style (default = linear), options = linear, nested
Please test and let me know any feedback or bugs.
Thanks for your work on this. In honor of how nasty listing subcategories has been all this time, I nominate a new name: Curse You, Subcategories. That hints at the recursion, as well as previous frustration. Suggested usage:
{exp:curse_you_subcats parent_id="29" show_parent="1"}{category_name}{/exp:curse_you_subcats}
😉
On a real note, I’ll give it a shot as soon as I hop back on the project that needs it.
If I may make another suggestion, could you put the version number of the release in the zip file name so we know whether we’re grabbing the right one or not?
recursive_subcats_110.zip or something similar?
Hi Mark,
It seems to me that such plugin as Recursive Subcategories should support a variable pair, for example {recursive_subcategories}{/recursive_subcategories} which would mark area parsed by plugin - simmilar to {categories}{/categories} variable pair which is supported by EE tags exp:weblog:entries and exp:weblog:category_archive.
Such variable pair would allow to have more control over parse order.That is, having such variable pair it would be possible to wrap with exp:recursive_subcategories tag some EE code and use
parse="inward"
parameter as here:
{exp:recursive_subcategories parent_id="29" show_parent="1" parse="inward"}
Some EE code
{recursive_subcategories}
{category_name}
{/recursive_subcategories}
Some EE code
{/exp:recursive_subcategories}
Here at first exp:recursive_subcategories would be parsed and then - tags belonging to “Some EE code”.
Also I think that some variable, say {category_level}, is needed which would output the number marking the level of category. Such variable would output number “0” for parent category, the number “1” for its subcategory, number “2” for subsubcategory, etc.
And the question: what does parameter show_parent=”1” mean? If it means that parent category should be displayed, then show_parent=”yes” is more intuitive.
I don’t think I can get this working at all. Here’s my code:
{exp:recursive_subcategories parent_id="111" show_parent="1" sort_by="cat_order"
style="linear"}{category_name}{/exp:recursive_subcategories}
This is embedded in a loop. I was embedding the first parameter, but decided to hardcode it instead just to get something to show up. I consistently get one result, which is category 29’s title. Utterly baffled about that. Weird thing is: parent_id=”29” is what’s used in the example inside the plugin itself.
Something is definitely up with this. Now matter what number I put in the parent_id parameter the plugin returns “29” - which is giving me the one result I happen to have for that category_id. Is 1.1.0 at the top of this thread the newest version?
EDIT: Looks like line 165 is hardcoded with a category number?
$this->get_nested(29,1);
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.