Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2025-02-03 15:59:08

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

Can I find out which category level/parent I'm at?

Given a bunch of nested category trees, are there any neat tag tricks I can employ to know if an article has its category assigned to a particular level? e.g if category2 is assigned to the root, or has a particular parent? Or in fact, has any parent, because then I can infer that the article either doesn’t have a category or is assigned the top level category node. The only way I can think of doing that last one is:

Type (used in category1)
— thing A
— thing B
— thing C
Location (used in category2)
— Area 1
—— Place 1
—— Place 2
—— Place 3
— Area 2
—— Place 4
—— Place 5
— Area 3
—— Place 6

If I’ve assigned an article category2 to “Place 4”, I want to get its parent name so I can check if it matches a particular value and then take a different action if so. That is distinct from the situation if the article category is assigned merely to Area 2 (which I use for stickies to display all articles in that group).

I’m passing a bunch of articles through a standard form shortcode and want to test inside that shortcode if certain category conditions are met to branch differently and display different stuff.

P.S. This is Txp 4.8.8 at the mo. I have a feeling this sort of thing is more easily doable in 4.9.

Last edited by Bloke (2025-02-03 15:59:54)


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

#2 2025-02-03 16:11:37

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,787
Website

Re: Can I find out which category level/parent I'm at?

Does your own smd_parent only work from within a category context given in the URL?
From reading the description, it would seem to be able to do the parent category retrieval, so if it’s not able to respond to an article category, maybe you can modify it to take txp:category1 and txp:category2 as input?


TXP Builders – finely-crafted code, design and txp

Offline

#3 2025-02-03 16:30:23

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

Re: Can I find out which category level/parent I'm at?

Hmmm. Honestly can’t remember if it’s only works on the global category tree or if it responds to article categories. I think it’s only the URL, but it’s been a while.

If it’s not possible to get at the parent/level info with native tags, I’ll take another look at the plugin and see what it can do.


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

#4 2025-02-03 16:37:19

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

Re: Can I find out which category level/parent I'm at?

Core category tags have parent attribute. I don’t remember how exactly they work, but you can try

<!-- immediate parent -->
<txp:if_category category='<txp:category2 />' parent="1" name="area_1, area_2, area_3" />

or

<!-- any level parent -->
<txp:if_category category='<txp:category2 />' parent name="area_1, area_2, area_3" />

Seemingly, <txp:category /> accepts parent attribute too:

<txp:category name='<txp:category2 />' parent="1" />

Offline

#5 2025-02-03 16:42:19

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,787
Website

Re: Can I find out which category level/parent I'm at?

If you have a limited number of areas, maybe you could use something like txp:category_list to output potentially matching place candidates to a variable or variables, and then use txp:if_variable with match="any" to establish whether your article category matches one of those.


TXP Builders – finely-crafted code, design and txp

Offline

#6 2025-02-03 16:58:10

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

Re: Can I find out which category level/parent I'm at?

Ahhh, yeah, I was pretty sure we had parent (and children?) attributes but they’re not yet documented. I didn’t think to try it with a value – I was foolishly using it valueless, expecting it to default to ‘immediate parent’.

Either way, it didn’t work with the <txp:category> tag as in your last example so maybe that’s 4.9.0+ only. But the <txp:if_category> seems to work on the surface, so I’ll run with that. Thank you.

EDIT: yep, for testing if an article is within any parent, the (documented, duh) <txp:if_category category='<txp:category2 />' parent="area1" /> works for testing individual parents.

Last edited by Bloke (2025-02-03 17:04:31)


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

#7 2025-02-03 17:11:15

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

Re: Can I find out which category level/parent I'm at?

Bloke wrote #338991:

Either way, it didn’t work with the <txp:category> tag as in your last example so maybe that’s 4.9.0+ only.

Strange, the code looks 4.8-ish and works for me.

EDIT: yep, for testing if an article is within any parent, the (documented, duh) <txp:if_category category='<txp:category2 />' parent="area1" /> works for testing individual parents.

Ah yes, it works this way too, even with a list of parents.

Offline

#8 2025-02-03 18:07:34

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

Re: Can I find out which category level/parent I'm at?

etc wrote #338992:

Strange, the code looks 4.8-ish and works for me.

Might just be me then. I was in a hurry and threw a rudimentary test on a live page and had to take it off quick! I’ll do more testing later.


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

#9 2025-02-03 18:38:10

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

Re: Can I find out which category level/parent I'm at?

Given the tree above and category2 set to ‘area1’ on this site:

<txp:category name='<txp:category2 />' parent="1" />

returns ‘area1’ 🤔

I’ve tried parent="-1" and parent="-2" as well. Hmmm.

I also can’t find a way to reliably detect if we’re at the topmost (first) level (i.e. parent is empty):

<if::category category='<txp:category2 />' parent="1" name="" />
<if::category category='<txp:category2 />' name="" />

Both give the same output whether I leave category2 empty, or set it to the first value (area1).


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 2025-02-03 19:30:19

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

Re: Can I find out which category level/parent I'm at?

Bloke wrote #338994:

Given the tree above and category2 set to ‘area1’ on this site:

<txp:category name='<txp:category2 />' parent="1" />...

returns ‘area1’ 🤔

Then you must be right, it might be a 4.9 thing.

I also can’t find a way to reliably detect if we’re at the topmost (first) level (i.e. parent is empty):

Have you tried

<txp:if_category category="algebra" parent name />|

There is also level attribute, but now I’m not sure whether it works in 4.8. Time to release 4.9?

Offline

#11 2025-02-03 23:18:58

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

Re: Can I find out which category level/parent I'm at?

etc wrote #338995:

Have you tried <txp:if_category category="algebra" parent name />

Ah. Yeah that works, thank you.


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

Board footer

Powered by FluxBB