Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-04-19 09:28:51

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,091
Website GitHub Mastodon Twitter

[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

#2 2009-04-19 09:39:01

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

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

#3 2009-04-19 09:41:36

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,091
Website GitHub Mastodon Twitter

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

#5 2009-04-19 09:47:27

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,091
Website GitHub Mastodon Twitter

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

#8 2009-04-19 11:08:27

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,091
Website GitHub Mastodon Twitter

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 the section and this_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

#9 2009-04-19 11:59:48

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: [howto] static category urls

I published Yiannis’ tip earlier today over here.

Offline

#10 2009-04-19 13:26:54

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

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:

  1. You can exclude categories
  2. 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

#12 2009-04-19 17:05:24

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,091
Website GitHub Mastodon Twitter

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 lis

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

Board footer

Powered by FluxBB