Textpattern tips, tutorials and code snippets

Vary the presentation of content within a section

Sometimes in a Textpattern project one needs to present content within the same section differently. There are a few ways to do this:

  1. Use articles and the if_article tag to change the presentation based on an article id
  2. Use categories and the if_category tag to change the presentation based on categories
  3. Use sections and the if_section tag to change the presentation within the same page template
  4. Use the override form under Advanced Options in the write tab

Method 1 example

The first method essentially hard codes an article into your page template. If you control the site this method works fine, but if you are building for a client you should expect the possibility that an article may be deleted or hidden, rendering this method useless.

<txp:if_article_id id="90">
<txp:article limit="1">
</txp:if_article_id>

Method 2 example

Using categories works best when you need to define multiple content, but does not work so well if you wish to simply change the presentation of one article.

<txp:if_category name="category_name">
...your content
<txp:else />
...some other content
</txp:if_category>

Method 3 example

Using sections works well, but like the second method, seems like overkill for only changing the presentation of one article.

<txp:if_section name="section_name">
<txp:if_individual_article>
<txp:article limit="1" />
<txp:else/>
<txp:article limit="10" />
</txp:if_individual_article>
</txp:if_section>

Using override form

The override form is an intriguing choice, and probably is one of Textpattern’s hidden secrets. It works well for simple changes of article presentation, but in this project I needed to change the entire page presentation which required a page template approach.

In a recent project, none of above methods seemed intuitive, so I came up with another method – use if_article_id, a second section and some articles, then pull the results into the first section for presentation. Here’s how to do it:

Create the second section

In our example, about is the name of the main section while staff is the second section. In the Sections tab, create a section called staff and set all options to no.

Create some articles

Go to the Write tab and create an article with the title staff, but do not enter anything into the body. Save the article to the about section.

Now, create some articles and save them into the staff section. These articles will be displayed differently from other articles in the about section.

Place a call to the content

In your about page template place the following code:

<txp:if_article_id id="90">
<txp:article_custom section="staff" limit="10">
<div class="width33 left margin20">
<h2><txp:title /></h2>
<txp:article_image />
<div class="height250">
<txp:body />
</div>
</div>
</txp:article_custom>
<txp:else />
<txp:article limit="1">
<div class="width66 left">
<h2><txp:title /></h2>
<txp:body />
</div>
</txp:article>
<div class="width33 right">
<txp:article_custom section="about" wraptag="ul" class="news_list" limit="10">
<li><a href="<txp:permlink />" <txp:if_article_id> class="active" </txp:if_article_id>><txp:title /></a></li>
</txp:article>
<!-- end width33 right -->
</div>
</txp:if_article_id>

Most of the final code is included here to demonstrate the content is manipulated based on whether or not the staff article is being returned – <txp:if_article_id id="90">.

What the code is doing is check if we are on the staff individual article page. If we are, then pull in 10 articles from the staff section. If we are not, then just display 1 article from the about section.

To see this code in action, visit the Spain Tennis Academy about page then click on the Staff link.

Use Textile help to style your comments