Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2015-07-10 08:05:15

BKawalec
Member
From: Poland
Registered: 2011-04-11
Posts: 23
Website

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

#2 2015-07-10 08:17:12

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,011
Website GitHub Mastodon Twitter

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

#3 2015-07-10 09:03:22

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Checking if category has children

colak wrote #293005:

Would this help?

It will, but yields an extra (and unnecessary) db query per category. You can find better. ;)

Offline

#4 2015-07-10 09:11:37

BKawalec
Member
From: Poland
Registered: 2011-04-11
Posts: 23
Website

Re: Checking if category has children

Yes, it’s exactly what I needed.
Thanks!

Offline

#5 2015-07-10 09:53:55

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,137
GitHub

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

#6 2015-07-10 11:30:12

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

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

#7 2015-07-10 11:31:34

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,137
GitHub

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

#8 2015-07-10 11:39:13

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Checking if category has children

gaekwad wrote #293039:

I meant it in a good-spirited way, sir. No malice intended.

No offense taken nor expected, sir. I’m not this kind of guy. :)

Offline

#9 2015-07-10 11:51:05

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,270
Website GitHub

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

#10 2015-07-10 14:15:15

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

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

Board footer

Powered by FluxBB