Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-07-27 14:58:04

shadden
New Member
Registered: 2010-07-27
Posts: 8

if category has only 1 article, show article instead of category list

this may be a very simple tag, but i’m new to textpattern –

i have a list of categories that are tied to each of my sections. i’m using the category list tag so that if someone clicks on one of the categories, it displays the list of articles for that category. within that tag, i want to include a conditional argument for when the category selected only has 1 article associated with it. in that case, i want the single article to display instead of the category list for that category. is this possible?

Offline

#2 2010-07-27 21:37:05

merz1
Member
From: Hamburg
Registered: 2006-05-04
Posts: 994
Website

Re: if category has only 1 article, show article instead of category list

Never did that but to me my solution sounds doable :)
(It’s already pretty late so forgive me if my logic breaks your system, kills a cat etc.)

  1. Inside an if_article_list lets start with a
  2. if_category condition and then play around with a combination of
  3. if_first_article AND
  4. if_last_article
  5. to double wrap around an article form.

Inside that pretty sick logical nesting 2x txp:else will then cover the rest :-)

Reference: textbook Category:Tag_Reference


Get all online mentions of Textpattern via OPML subscription: TXP Info Sources: Textpattern RSS feeds as dynamic OPML

Offline

#3 2010-07-27 21:52:23

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: if category has only 1 article, show article instead of category list

I want the single article to display instead of the category list for that category. is this possible?

I think you wanted to mean “I want the single article to display instead of the article list for that category”.

In any case, if Markus’s sleepy solution doesn’t work, you could try this:

<txp:variable name="has_more_than_one_article" value='<txp:article limit="1" offset="1" />' />
<txp:if_variable name="has_more_than_one_article" value="">
<!-- ok, there is just one article, display it full -->
  <txp:article>.. some txp tags for displaying your article chunks, like body, title, etc... </txp:article>
<txp:else />
<!-- oK, it seems there is more than one article, lets show 10 of them -->
  <txp:article limit="10">... some txp tags for displaying your article chunks, like title, excerpt, permlink, etc ...</txp:article>
</txp:if_variable>

Basically, you are creating a variable, and using a nasty trick (I think Els is the one who came up with it) to check if there is more than one article assigned to current category. How? By using the offset="1" attribute. It just means: “look for the second article”, and if there is no second article, it will return empty. Then, we use if_variable to test if the variable is empty, or (else) if it has something.

Edit: fixing error in code, thanks jsoo.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#4 2010-07-27 22:05:15

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

Re: if category has only 1 article, show article instead of category list

maniqui wrote:

Basically, you are creating a variable, and using a nasty trick (I think Els is the one who came up with it)

Elegant, not nasty!

Note that the second attribute in the first line of Julián’s code is value, not name again.


Code is topiary

Offline

#5 2010-07-27 22:36:58

shadden
New Member
Registered: 2010-07-27
Posts: 8

Re: if category has only 1 article, show article instead of category list

Thanks all; I used Julian’s code with Jeff’s update and it does the trick. Not sure if that’s exactly how I should be structuring my site, but I’m still learning…

Offline

#6 2010-07-28 13:08:09

merz1
Member
From: Hamburg
Registered: 2006-05-04
Posts: 994
Website

Re: if category has only 1 article, show article instead of category list

maniqui Hat tip. Much better :-)


Get all online mentions of Textpattern via OPML subscription: TXP Info Sources: Textpattern RSS feeds as dynamic OPML

Offline

#7 2010-07-31 08:56:54

Katalonian
Member
From: Baku, Azerbaijan
Registered: 2010-04-18
Posts: 219
Website

Re: if category has only 1 article, show article instead of category list

<txp:if_individual_article> </>


<txp:txp_me />

Offline

#8 2010-07-31 09:36:06

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

Re: if category has only 1 article, show article instead of category list

Katalonian wrote:

<txp:if_individual_article> </>

Ziya, excuse me, but what should your post mean? :-)

Offline

Board footer

Powered by FluxBB