Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2009-01-05 12:31:05
- psimsy
- Member
- From: Londinium
- Registered: 2004-12-27
- Posts: 13
Category list. Linking and non-linking
I am trying to get txp to display a list of ALL categories, but only link to categories that have articles assigned to them. So the list can grow with the addition of new categories and then become linked to content when it is created.
I have looked at hacking plugins (like azp_menugen) and trying to use conditionals but not had much luck..
Any ideas would be gratefully received.
p_
Offline
Re: Category list. Linking and non-linking
The built-in tag category_list with some conditional cleverness might help — and one of the tag gurus might well have answered this before or know of a really cool native solution.
Failing that, do the adi_cat_menu or cbs_category_list plugins get you anywhere?
Last edited by Bloke (2009-01-05 12:40:52)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
#3 2009-01-05 15:34:10
- els
- Moderator

- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: Category list. Linking and non-linking
Bloke wrote:
— and one of the tag gurus might well have answered this before or know of a really cool native solution.
Did I hear my name? ;)
<txp:category_list wraptag="ul" break="li">
<txp:variable name="hasarticles" value='<txp:article_custom category=''<txp:category />'' limit="1" />' />
<txp:if_variable name="hasarticles" value="">
<txp:category title="1" />
<txp:else />
<txp:category title="1" link="1" />
</txp:if_variable>
</txp:category_list>
Last edited by els (2009-01-05 15:35:50)
Offline
Re: Category list. Linking and non-linking
Genius! You tag fiend, you ;-)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
Re: Category list. Linking and non-linking
wow. i’m very very very slowly starting to grasp the possibilities that txp:variable gives!
~~~~~~~~~~~~~| monolinea.com | pixilate.com | istockphoto.com/kemie |~~~~~~~~~~~~~
Offline
#6 2009-01-06 10:48:00
- psimsy
- Member
- From: Londinium
- Registered: 2004-12-27
- Posts: 13
Re: Category list. Linking and non-linking
Hi. Thank you for the great help. For the sake of anyone else that needs this I am posting some extra how to use info.
I put code in a form and called the form into a page. It didn’t seem to work called from an article.
Here is the slightly hacked version I used to for my purposes. Since <txp:category_list> doesn’t nest the categories, I had to break up the list into the multiple category chunks I needed and put them all in the form. This is a bit bulky and non-DRY and I am sure there is a better way out there.
I am concerned that with the addition of <txp:variable> which seems amazingly powerful we are going to lose the simplicity of the tag & plugin relationship. But i expect this discussion is raging elsewhere.
Thanks for your help.
p_
<txp:category_list wraptag="ul" break="li" parent="health" children="0" class="list" exclude="health" >
<txp:variable name="hasarticles" value='<txp:article_custom section="articles" category=''<txp:category />'' limit="1" />' />
<txp:if_variable name="hasarticles" value="">
<txp:category title="1" />
<txp:else />
<a href="articles/<txp:category />"><txp:category title="1" /></a>
</txp:if_variable>
</txp:category_list>
Offline
#7 2009-08-28 11:59:36
- txpnisti
- Member
- Registered: 2009-02-25
- Posts: 23
Re: Category list. Linking and non-linking
I found the orignal code above yesterday, it solves nicely the problem listing only used categories even on MLP-site like I have. But why it adds so much queries? When looking the source debug mode on, without this I have 25 queries on page, and when this is activated there is 70. Isn’t that little bit too much? Is there a workaround to reduce the amount?
It doesn’t seem to affect loading time but I’m currently the only one visiting the site under development. I’m not sure if that is too much when there is lots of visitors browsing?
Offline
Re: Category list. Linking and non-linking
This is really useful, thanks Els for your TXP tag genius
Admin Themes Prometheus | Stung | <txp:coder/
My Portfolio | ɹǝpuɐz.com | @MrMartineau
<txp:coder /> – Convert your designs into Textpattern-based websites
jQuery Style | @jquerystyle
Offline
Re: Category list. Linking and non-linking
I was exited to find this Els Code and was hoping it would do what i needed.
I want to output a list of categories with links to the articles and limited to one section. I only want category names of the parent to be listed, with link to the article.
I have this code working just fine, except it ouputs empty category names, with no articles assigned to them which is not ideal:
<li><txp:category_list exclude="notebook" section="notebook" break="li" wraptag="ul" parent="notebook" children /></li>
In using the Els Code:
<txp:category_list wraptag="ul" break="li" parent="notebook" class="list" exclude="notebook" >
<txp:variable name="hasarticles" value='<txp:article_custom section="notebook" category=''<txp:category />'' limit="1" />' />
<txp:if_variable name="hasarticles" value="">
<txp:category title="1" />
<txp:else />
<a href="notebook/<txp:category />"><txp:category title="1" /></a>
</txp:if_variable>
</txp:category_list>
It kind of works. But it ouputs all the category names even ones without assigned articles, with the exception that the empty categories names are not links… only the assigned categories are active links to articles.
But the urls produced are incorrect: http://piovedue.local/notebook/articles which lead to 404 Not Found.
Whereas in the first code listed it produces correct links to articles matching the category: http://piovedue.local/notebook?c=articles
I have not been able to modify the Els Code to:
A) show only categories with articles assigned to them, and NOT show empty categories and
B) have proper link to the article.
PS there seems to be a dearth of documentation on this issue. How to display a list of categories, only if they have an articles assigned to them by section, with a link to the full article.
…. texted postive
Offline
#10 Today 01:46:00
Re: Category list. Linking and non-linking
bici wrote #342961:
… but it ouputs all the category names even ones without assigned articles, with the exception that the empty categories names are not links… only the assigned categories are active links to articles.
This is your clue: if you look at both alternatives in your if_variable code, you are outputting the category title in both. One of them is wrapped in a link, the other just output as the title.
But the urls produced are incorrect:
http://piovedue.local/notebook/articleswhich lead to 404 Not Found.
Here, too, your code gives you your answer:
<a href="notebook/<txp:category />">…
This is what is producing your link.
So, what you want is:
<txp:category_list wraptag="ul" break="li" parent="notebook" class="list" exclude="notebook">
<txp:variable name="hasarticles" value='<txp:article_custom section="notebook" category=''<txp:category />'' limit="1" />' />
<txp:if_variable name="hasarticles" value="" not>
<a href="notebook/?c=<txp:category />"><txp:category title="1" /></a>
</txp:if_variable>
</txp:category_list>
The differences are:
- Check for the
notcase, so you don’t have to check for nothing and else. - Drop the output when a category has no articles
- Modify the
hrefto output the link how you want it.
TXP Builders – finely-crafted code, design and txp
Offline
#11 Today 02:26:52
Re: Category list. Linking and non-linking
jakob wrote #342962:
This is your clue: if you look at both alternatives in your if_variable code, you are outputting the category title in both. One of them is wrapped in a link, the other just output as the title.
Here, too, your code gives you your answer:
<a href="notebook/<txp:category />">…...This is what is producing your link.
So, what you want is:
<txp:category_list wraptag="ul" break="li" parent="notebook" class="list" exclude="notebook">...The differences are:
- Check for the
notcase, so you don’t have to check for nothing and else.- Drop the output when a category has no articles
- Modify the
hrefto output the link how you want it.
Thank you!
I had no clue about value="" not>
I also had to change <a href="notebook/?c=<txp:category />"><txp:category title="1" /></a>
to <a href="notebook?c=<txp:category />"><txp:category title="1" /></a>
i.e eliminate the “/” so it went to the correct url
…. texted postive
Offline
#12 Today 08:11:29
Re: Category list. Linking and non-linking
bici wrote #342964:
eliminate the “/” so it went to the correct url
Yeah this is new in 4.9.x. There’s a pref for trailing slashes now, so you need to be careful about creating the correct links that match your pref or it may 404.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline