Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-03-08 12:48:49

mnewboult
New Member
Registered: 2010-01-05
Posts: 5

Redirect to article if there's only one article in a given category

I have the requirement to display a list of categories. Articles are all tagged with a specific category. If the user clicks a category with > 1 article, they need to see the list of articles to choose from. However if there’s only one article for that given category I need to redirect the user to that article – they shouldn’t have to choose anything.

Does anyone know if there’s a plugin for that?

Thanks

Offline

#2 2010-03-08 13:19:26

joebaich
Member
From: DC Metro Area and elsewhere
Registered: 2006-09-24
Posts: 507
Website

Re: Redirect to article if there's only one article in a given category

You shouldn’t need a plug-in for this, I think. See Els’ TXP tip here. Look at the section of her code headed by <!— check if there is more than one article in this section —>. You can readily adapt this method to identify article categories with only one article instead of sections with only one article.

Offline

#3 2010-03-08 13:24:28

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Redirect to article if there's only one article in a given category

Some tiny example without plugins:

<txp:variable name="count" value="0" />

<!--
	Count done with article_custom and variables.
-->

<txp:article_custom category='<txp:category />' limit="2">
	<txp:if_first_article>
		<txp:variable name="count" value="1" />
	<txp:else />
		<txp:variable name="count" value="2" />
	</txp:if_fist_article>
</txp:article_custom>

<!--
	Check the results from the contents of "count".
-->

<txp:if_variable name="count" value="1">

	<!--
		Fetch permlink.
	-->

	<txp:article_custom category='<txp:category />' limit="1">
		<txp:php>
			header(
				'Location: '.permlink(array())
			);
		</txp:php>
	</txp:article_custom>
</txp:if_variable>

Added some comments to the code. Hope it helps.

Offline

#4 2010-03-08 14:42:35

mnewboult
New Member
Registered: 2010-01-05
Posts: 5

Re: Redirect to article if there's only one article in a given category

Thank you very much. Worked a treat. Just starting to get under the bonnet (hood) of txp and find this kind of help very much appreciated.

The only bit about the above example I don’t get is:


header(
‘Location: ‘.permlink(array())
);

Offline

#5 2010-03-08 16:11:05

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Redirect to article if there's only one article in a given category

mnewboult wrote:

The only bit about the above example I don’t get is:

It’s PHP. Sends location header with <txp:permlink /> as contents.

Offline

Board footer

Powered by FluxBB