Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#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
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.)
- Inside an if_article_list lets start with a
- if_category condition and then play around with a combination of
- if_first_article AND
- if_last_article
- 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
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.
Offline
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
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
Re: if category has only 1 article, show article instead of category list
<txp:if_individual_article> </>
<txp:txp_me />
Offline
Offline