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: 7,323
Website

[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.org | hblack.net | LABS | State Machines | Respbublika! | NeMe @ github

Offline

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

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 8,761
Website

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: 7,323
Website

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.org | hblack.net | LABS | State Machines | Respbublika! | NeMe @ github

Offline

#4 2009-04-19 09:42:35

Els
Admin
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: 7,323
Website

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.org | hblack.net | LABS | State Machines | Respbublika! | NeMe @ github

Offline

#6 2009-04-19 09:55:44

Els
Admin
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
Admin
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: 7,323
Website

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.org | hblack.net | LABS | State Machines | Respbublika! | NeMe @ github

Offline

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

jstubbs
Moderator
From: Hong Kong
Registered: 2004-12-13
Posts: 2,394
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: 8,761
Website

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

Board footer

Powered by FluxBB