Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
List only categories that contain content
Am I missing something here? Using <txp:category_list />
is there any neat way to exclude categories that don’t (yet) have content — eg. articles — assigned to them? Or do I need to make a query up-front to grab only the ones that contain content and then check if the current category in the loop is in that list? Or… faints… make a query per iteration of the loop to establish the article count in each category and omit it if it’s empty.
I feel I must be missing something obvious here.
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: List only categories that contain content
This seems to be one of these questions where the optimal answer depends on what you want to output: just category names? with titles? with article counts? etc. Generally, linking to content (e.g. articles) is unavoidable, since txp_category
table has no content_count
field.
Offline
Re: List only categories that contain content
Just titles, ostensibly. Counts might be useful, but not essential. I just want to omit any that don’t have any content. Or maybe grey them out or write (0) after them or… something. Whatever’s the most efficient way to achieve that.
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: List only categories that contain content
If you only consider Category1
(or are dealing with files/images/links nope, articles only), this might work:
<txp:article_custom category match="Category1" fields="Category1" sort="Category1" limit="0" wraptag="p" break="br">
<txp:category1 link title /> (<txp:yield item="count" />)
</txp:article_custom>
If you must consider also Category2
, things look more complicated. Anyway, retrieving titles this way yields a db query per category. If counts are optional, a less db-calls expensive alternative is to make first a used category list:
<!-- store it in some variable -->
<txp:article_custom category limit="0" wraptag="" break=",">
<txp:category1 />,<txp:category2 />
</txp:article_custom>
and then pass this list to <txp:category_list />
as categories
attribute.
Offline
Re: List only categories that contain content
etc wrote #338922:
If counts are optional, a less db-calls expensive alternative is to make first a used category list:
<!-- store it in some variable -->...
and then pass this list to
<txp:category_list />
ascategories
attribute.
I think this is what I’ve done in the past, except as a txp:php
snippet querying the db for just the category1 and category2 columns and then removing duplicates.
But I’m intrigued by the use of the category
attribute on its own and especially the limit="0"
. That’s new to me.
TXP Builders – finely-crafted code, design and txp
Offline
Re: List only categories that contain content
jakob wrote #338924:
querying the db for just the category1 and category2 columns and then removing duplicates.
This can be done adding fields="Category1, Category2"
, but it yields sql GROUP BY
and might be expensive, to test. Removing duplicates is not really necessary here because <txp:category_list />
does it anyway.
But I’m intrigued by the use of the
category
attribute on its own and especially thelimit="0"
. That’s new to me.
Maybe it’s a 4.9 thing, but valueless category
restricts the articles list to those with not empty Category1
or Category2
, according to match
attribute. And limit="0"
means no limit (4.9+ only?)
Offline
Re: List only categories that contain content
etc wrote #338922:
If counts are optional, a less db-calls expensive alternative is to make first a used category list:
Yeah, this was my go-to solution because it was less process-intensive than doing it in the loop. But then I had a crisis of conscience and thought there must be a better way.
Thank you. At least I’ve satisfied myself that I’m not missing some clever tag-fu.
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