Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2024-05-01 14:11:31

raminrahimi
Member
From: India
Registered: 2013-03-19
Posts: 278

How to list nested sub categories articles?

How I can list the articles of sub categories like I have the main category News and all other sub categories Politics, Security, Sports, etc… belong to News category.
Now I want to list the articles belong to all those sub or nested category of News -> politics, security, sports etc…
<txp:article_custom category="here-news-subcategories" limit="10>...

Offline

#2 2024-05-01 15:34:22

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

Re: How to list nested sub categories articles?

I usually do this in two steps:

  1. Get children categories of parent “news” and save in variable
  2. Output your article_custom tag with the variable as the category attribute
<txp:variable name="news_cats" trim>
    <txp:category_list parent="news" exclude="news" children="1" break=","><txp:category /></txp:category_list>
</txp:variable>
<txp:article_custom category='<txp:variable name="news_cats" />' limit="10" … />

Remember to use single quotes around the tag in the attribute.

(You can do this as a tag-in-tag without the variable but it quickly becomes hard to read.)

It wasn’t clear if you also want the parent “news” category too. If you do, just remove the “exclude” attribute. If you want multiple levels of sub-categories, use the attribute children="2" (or however many levels you want). See the docs for category_list for more information


TXP Builders – finely-crafted code, design and txp

Offline

#3 2024-05-01 19:07:13

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

Re: How to list nested sub categories articles?

As usual, there are many ways, you should describe your requirements more in details. Probably, depth will do?

<txp:article_custom category="news" depth="1" ... />

Offline

#4 2024-05-02 07:36:30

Dragondz
Moderator
From: Algérie
Registered: 2005-06-12
Posts: 1,538
Website GitHub Twitter

Re: How to list nested sub categories articles?

Hi Oleg

What the “depth” attribute ?

I searched doc site for “depth” i got nothing, is it mentioned somewhere ?

Thanks

Offline

#5 2024-05-02 08:54:07

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

Re: How to list nested sub categories articles?

Hi Rabah,

Dunno why depth attribute of <txp:article(_custom) /> is undocumented. Maybe it’s a 4.9 thing, maybe an oblivion. The idea is to match the subcategories of certain levels:

  • category="news" depth="0" (default) matches news category
  • category="news" depth="1" matches news children
  • category="news" depth="1-2" matches news children and grand-children
  • category="news" depth matches news and its descendants

and so on. Negative depth might match parents too, I don’t remember.

Offline

#6 2024-05-02 09:55:49

Dragondz
Moderator
From: Algérie
Registered: 2005-06-12
Posts: 1,538
Website GitHub Twitter

Re: How to list nested sub categories articles?

Thanks Oleg,

I think it s super useful.

The re is no mention about depth for article_custom in History.txt then i dont know when it has been set.

Cheers.

Offline

#7 2024-05-02 13:53:50

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

Re: How to list nested sub categories articles?

Yes, it can be handy. If history serves, depth was introduced in 4.8.0, but the docs did not follow. This guy’s fault (kidding :-)

Offline

Board footer

Powered by FluxBB