Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
[howto] static category urls
If the How tos and examples forum was open, I would post this thread there as I believe that it is the most appropriate place where tag based solutions can be developed and discussed.
The problem
Further to a a similar post i wrote some time ago (can’t find it so as to link to it), I came up with the following code so as to generate a list of categories linked to context insensitive urls.
TXP by default has many url schemas for categories so if one uses <txp:category_list wraptag="ul" break="li" />
you will end up with urls like
- yoursite.tld/category/yourcategory/
- yoursite.tld/whatever_section/?c=yourcategory
- yoursite.tld/whatever_other_section/?c=yourcategory
- etc
This is OK if one wants to have context sensitive categories but not if one is trying to ‘fake’ a static site. I also think that the build in method creates a lot of duplicate content and also makes the designing of that content slightly more difficult.
The solution
Checking for categories with a minimum of 1 article and then returning a list of ‘populated’ categories.
<ul id="categorylist" class="categorylist">
<li><a href="<txp:site_url />">Home</a></li>
<txp:variable name="hasarticles" value='<txp:article_custom category=''<txp:category />'' limit="1" />' />
<txp:if_variable name="hasarticles" value="">
<txp:else />
<txp:article_custom form="menu" limit="9999" sortby="Category1,Title" sortdir="asc" />
</txp:if_variable>
</ul>
and the menu form contains:
<txp:if_different>
<txp:category1 title="1" link="1" wraptag="li" />
</txp:if_different>
If your htaccess file works fine, the code returns an unordered list of linked categories using only the yoursite.tld/category/yourcategory/
schema.
I believe that the above method works well but there is a caveat: It does not allow for exclusion of any populated categories, a feature possible with the category_list
tag.
Credits
the hasarticles
sniffer was taken from one of Els’ invaluable responses in this forum whereas the if_different
part is appropriated from Jeremie
The question
Using the method above is there a way to exclude a populated category from the list?
>Edit — refining the code
Last edited by colak (2009-04-19 15:29:51)
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: [howto] static category urls
Cool. You might consider posting this to TXP Tips if you haven’t already. I’m sure it’ll help some folk out who make static-like sites.
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: [howto] static category urls
Bloke wrote:
Cool. You might consider posting this to TXP Tips if you haven’t already. I’m sure it’ll help some folk out who make static-like sites.
Hi Stef, Did that last night. Not sure if it’ll be published but thought of posting here so as to complete the code and to allow for excluded
categories.
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
#4 2009-04-19 09:42:35
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: [howto] static category urls
I just discovered that though the How-tos forum is closed threads can still be moved there :)
Offline
Re: [howto] static category urls
Els wrote:
I just discovered that though the How-tos forum is closed threads can still be moved there :)
Hi Els,
Cool. but can forum members post and contribute to the threads though?
Last edited by colak (2009-04-19 09:48:35)
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
#6 2009-04-19 09:55:44
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: [howto] static category urls
Oops, I didn’t think of that… I don’t know really. Stef, are you still around to give it a try?
If it’s not possible, we’d better report this thread to ask if it can be moved back. Apologies Yiannis!
Offline
#7 2009-04-19 10:14:21
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: [howto] static category urls
Yiannis, I’ve read and re-read your post, and I’m probably missing something, but doesn’t <txp:category_list />
without the section
and this_section
attribute always return /category/yourcategory URLs?
Offline
Re: [howto] static category urls
Els wrote:
Yiannis, I’ve read and re-read your post, and I’m probably missing something, but doesn’t
<txp:category_list />
without thesection
andthis_section
attribute always return /category/yourcategory URLs?
Hi Els, unfortunately no. I have tried to get a category list with static urls using
<txp:category_list wraptag="ul" break="li">...</txp:category_list>
<txp:category_list this_section="0" wraptag="ul" break="li">...</txp:category_list>
<txp:category_list this_section="1" wraptag="ul" break="li">...</txp:category_list>
- etc
but every time the urls returned were context sensitive
Last edited by colak (2009-04-19 11:09:53)
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Offline
Re: [howto] static category urls
jstubbs wrote:
I published Yiannis’ tip earlier today over here.
Nice one. And if this post exists then it also answers Els’ question from earlier :-)
Last edited by Bloke (2009-04-19 13:27:19)
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
#11 2009-04-19 16:36:36
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: [howto] static category urls
colak wrote:
but every time the urls returned were context sensitive
I tried it on my test site. The tag used is <txp:category_list wraptag="ul" break="li" />
and it returns /category/yourcategory links from everywhere…
Edit: ah, now I see what’s happening: when used as a single tag it always returns /category/yourcategory links, but when used as a container tag it’s context sensitive… I find that a very strange behaviour.
Edit again: Yay, found it! This code returns context insensitive links:
<txp:category_list wraptag="ul" break="li">
<txp:category title="1" link="1" section="default" />
</txp:category_list>
Advantages over your example:
- You can exclude categories
- No need for the article_custom/if_different way, which I assume needs more queries
Last edited by els (2009-05-31 21:11:10)
Offline
Re: [howto] static category urls
Yep, that works!!! The txp tag God has done it again! So to appropriate your code again the final thing would be:
<ul id="categorylist" class="categorylist" >
<li><a href="<txp:site_url />">Home</a></li>
<txp:category_list break="li" exclude="somecategory,someothercategory">
<txp:variable name="hasarticles" value='<txp:article_custom category=''<txp:category />'' limit="1" />' />
<txp:if_variable name="hasarticles" value="">
<txp:else />
<txp:category title="1" link="1" section="default" />
</txp:category_list>
</txp:if_variable>
</ul>
> Edit Maybe I spoke too soon:( for some reason the above code doesn’t work as it generates empty li
s
ie the menu looks something like
<ul id="categorylist" class="categorylist" >
<li><a href="#">Home</a></li>
<li><a href="#">C1</a></li>
<li></li>
<li><a href="#">C2</a></li>
<li></li>
<li><a href="#">C3</a></li>
<li></li>
...
</ul>
Last edited by colak (2009-04-19 17:38:18)
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline