Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
Checking if category has children
Is there a way to check if a category has subcategories? I’m building a dropdown menu of categories with their children (at present three levels deep). I need to give parent categories a css class and prevent them from being links.
Offline
Re: Checking if category has children
Hi,
Would this help?
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Offline
Re: Checking if category has children
Yes, it’s exactly what I needed.
Thanks!
Offline
Re: Checking if category has children
etc wrote #293007:
It will, but yields an extra (and unnecessary) db query per category. You can find better. ;)
I detect an elevator pitch for etc_query
…
Offline
Re: Checking if category has children
gaekwad wrote #293015:
I detect an elevator pitch for
etc_query
…
No, Pete, I’m not this kind of guy. The idea is to use <txp:variable />
as container, not just as a flag. For one level this would be
<txp:category_list wraptag="ul" break="li" children="0">
<txp:variable name="children"><txp:category_list wraptag="ul" break="li" parent='<txp:category />' exclude='<txp:category />' children="0">
<txp:category title="1" link="1" />
</txp:category_list></txp:variable>
<txp:category title="1" link='<txp:if_variable name="children" value="">1<txp:else />0</txp:if_variable>' />
<txp:variable name="children" />
</txp:category_list>
But for multiple levels it quickly gets complicated. An amazingly compact solution is
<txp:etc_tree circular="3" wraptag="" break="">
<txp:category_list wraptag="ul" break="li" parent='<txp:category />' exclude='<txp:category />' children="0">
<txp:variable name="children"><txp:etc_tree /></txp:variable>
<txp:category title="1" link='<txp:if_variable name="children" value="">1<txp:else />0</txp:if_variable>' />
<txp:variable name="children" />
</txp:category_list>
</txp:etc_tree>
OK, I’m (sometimes) this kind of guy.
Offline
Re: Checking if category has children
etc wrote #293038:
No, Pete, I’m not this kind of guy.
[…]
OK, I’m (sometimes) this kind of guy.
I meant it in a good-spirited way, sir. No malice intended.
Offline
Offline
Re: Checking if category has children
This is one of those things where a pre-order binary tree isn’t a fabulous choice of structure when mapped onto our current iterative tags. It’s possible to do it, just not very efficiently with the current tag suite.
Offers accepted from anyone to improve the output of the tags, perhaps with suitable meta data added to $thiscategory
, so it becomes easier and less resource intensive to make level-based decisions.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Checking if category has children
Absolute level and children, as given by getTree() function, can be easily calculated. But categories and exclude attributes of <txp:category_list />
can make these results look skewed. The difficulty is mainly conceptual: what is a category tree with some category excluded? Should we exclude their subcategories too? And so on.
Offline
Pages: 1