Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-06-30 03:04:37

dreamer
Member
Registered: 2007-06-08
Posts: 242

conditional statement to show a list of articles

I want to do the following:

A user goes to a section and will see the latest articles for that section. But if they click on a specific link from outside of that section, then it will show them just that entry. This is what I have but haven’t quite got it. The problem is when I click on a specific link, I keep seeing the same article despite a different link.

My site here I am trying to do this for News/Events. It works properly for the link in the navigation, but it doesn’t work with the links under the logo.

        <txp:article>
     		 <txp:if_article_list>
<txp:article_custom allowoverride="0" form="article-news-events" limit="7" pgonly="0" section="news-events" sort="Posted desc" status="4" />
                 <txp:else />
<txp:article_custom form="article" limit="1" pgonly="0" section="news-events" status="4" />
</txp:if_article_list>
</txp:article>

and the form, article-news-events has this;

 <h3><txp:title /></h3>
<txp:body />

Offline

#2 2010-06-30 04:00:49

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

Re: conditional statement to show a list of articles

hi dreamer,

there is something conceptually wrong in the code, about how txp:article and txp:article_custom works.
txp:article is context-sensitive, which means it will “react” to the context, like an article list (a section, a category, a search) or an individual article (its permalink), while txp:article_custom isn’t context-sensitive, which means it just brings a list of articles based on the attributes you fed into it.

You may want to try this code (read the comments for more info about how it works)

<txp:if_article_list>
  <txp:article allowoverride="0" form="article-news-events" limit="7" />
  <!-- 
    You are in an article list, in this case, in the frontpage for section "news-events".
    I've changed article to article_custom and removed some attributes 
    as they were set to their default values.
    As you are already in section "news-events", the txp:article will
    "know" that, and will then just fetch articles from that section.
   -->
<txp:else />
   <txp:article form="article" />
  <!-- 
    You are in an individual article. 
    Again, I've changed article to article_custom and removed some attributes 
    as they were set to their default values.
    As you are already at an article's permalink, the txp:article will
    "know" that, and will just fetch the corresponding article for that permalink.
   -->
</txp:if_article_list>

In your original code, the wrapping <txp:article></txp:article> wasn’t necessary, and in fact, wasn’t correct the way it was being used.

Of course, the code I’ve posted above is not the only way to do this.
Let’s see another way: in a similar fashion to your original code, you could have done something like the following:

<txp:article limit="7">
<!-- 
  Let's take advantage of txp:article context-sensitiveness.
  The above tag (which is closed at the end of this snippet) will "react" 
  to the context. 
  In an article list context, it will fetch 7 articles.
  In an individual article context, it will fetch just 1 article, the one corresponding to the URL.
  <txp:if_article_list>
    <txp:output_form form="article-news-events"  />
  <!-- 
    You are in an article list, in this case, in frontpage for section "news-events".
    For each of the 7 fetched articles, lets use the "article-news-events" form to render them.
   -->
  <txp:else />
  <!-- Individual article -->
     <txp:output_form form="article"  />
  <!-- 
    You are at individul article context, aka permalink.
    For the only fetched article, let use the "article" form to render it.
   -->
  </txp:if_article_list>
</txp:article>

I would say that this second snippet, although it should work correct, it feels somewhat wrong. I mean, I just don’t like it…

dreamer, there are even other possible ways to achieve the same result, but first, let’s see if any of those snippets works for you.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#3 2010-06-30 04:37:32

dreamer
Member
Registered: 2007-06-08
Posts: 242

Re: conditional statement to show a list of articles

ah….i’ve seen the light. would this be correct as well…. including the <txp:if_individual_article> tags within the <txp:if_article_list> tag?

 <txp:if_article_list>
<txp:article allowoverride="0" form="article-news-events" limit="7" pgonly="0" sort="Posted desc" status="4" />
                  </txp:if_article_list>
               <txp:else />
<txp:if_individual_article>
<txp:article form="article" limit="1" pgonly="0" status="4" />
</txp:if_individual_article>

Offline

#4 2010-06-30 04:45:53

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

Re: conditional statement to show a list of articles

Not necessarily, and in fact, you ended up with slightly incorrect code: now, you have a “remaining” txp:else.

This are the three “correct” ways:

 <txp:if_article_list>
  <txp:article allowoverride="0" form="article-news-events" limit="7" pgonly="0" sort="Posted desc" status="4" />
</txp:if_article_list>

<txp:if_individual_article>
  <txp:article form="article" limit="1" pgonly="0" status="4" />
</txp:if_individual_article>

The above one is a bit suboptimal. It will be checking both things, but just one will be true.

<txp:if_article_list>
  <txp:article allowoverride="0" form="article-news-events" limit="7" pgonly="0" sort="Posted desc" status="4" />
<txp:else />
  <txp:article form="article" limit="1" pgonly="0" status="4" />
</txp:if_article_list>

Now, the above is a bit more correct.

<txp:if_individual_article>
  <txp:article form="article" limit="1" pgonly="0" status="4" />
<txp:else />
  <txp:article allowoverride="0" form="article-news-events" limit="7" pgonly="0" sort="Posted desc" status="4" />
</txp:if_individual_article>

Finally, this last one is the “inverted” way of the previous one.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB