Textpattern : Categories

Textpattern logo

We have seen how to use the main elements of Textpattern: pages, sections, forms and articles.

There is one more element: categories. These are not essential to the functioning of a Textpattern website, but they allow us to refine how the website works.

Articles, Sections and Categories

A Textpattern article is located in the structure of the website by being assigned to a section.

There is not, however, always a direct correlation between an article’s subject matter and the section to which it is assigned. There may be occasions when you want to organise articles other than according to their sections:

  • It may be necessary for the purposes of a particular website for articles within the same section to be sorted or grouped according to their subject matter.
  • It may also be necessary to sort or group articles from different sections that have the same subject matter.

The Textpattern category feature allows you to do this.

Using Categories

Take the example of www.crawleysussex.co.uk. The bulk of this website consists of an online directory of businesses and other organisations based in and around Crawley in Sussex.

The directory itself comprises one section of the website. Within this section, the material is classified by category. Each entry in the directory is categorised by its type, such as butcher, baker or candlestickmaker (not many of those), and by its location.

The Textpattern category feature allows this one Textpattern page to act as a template for three types of HTML page:

  • a section landing page which includes a list of categories;
  • landing pages for each category, listing the entries that fall into each category;
  • individual pages for each business or other organisation.

Categorising Articles

Textpattern’s default installation allows you to place each article in up to two categories.

The first thing to do is to create the categories. Go to Content then Categories. Several types of category are available. We need the type that applies to articles:

Textpattern categories screen

Write the name of the category in the box, and click Create. The categories you create will then be visible in drop–down boxes on the Write page. Simply select the category or categories which apply to that article:

Textpattern category menu screen

Unlimited Categories Plug–In

In practice, two categories will not always be enough. You will usually find that you need either none or a lot more than two. Fortunately, there is an excellent plug–in that allows you to create an unlimited number of categories. It is called rss_unlimited_categories, and is available from the author’s website and the main Textpattern plug–in depository.

Categories and Landing Pages

By default, a Textpattern page will function as an HTML landing page, and display a list of all the articles assigned to the section which applies to that page.

We have seen how you can change the default settings so that the landing page displays a limited number of articles from that section, or displays articles from another section.

You can also change the default settings so that the landing page displays articles according to their category. To do so, you need to use this conditional statement:
<txp:if_category>
  <!-- category–specific landing page code goes here -->
<txp:else />
  <!-- non–category–specific landing page code here -->
</txp:if_category>

This is what the code tells Textpattern to do:

  • <txp:if_category>
    This means: if the visitor has landed here by activating a link to a category, use the following code to display the articles according to their categories.
  • <txp:else />
    This means: if the visitor has not landed here by activating a link to a category, use the following code to display the articles in the normal way.
  • </txp:if_category>
    This is simply the end of the conditional statement.

Landing Page by Category Only

If the landing page is to display the articles only according to their category, you still need to use the conditional statement. Just leave the second area blank:
<txp:if_category>
  <!-- category–specific landing page code goes here -->
<txp:else />
</txp:if_category>

Landing Page and Individual Article Page

If you want the Textpattern page also to display individual article pages, you will need to surround the code for the landing page with the standard conditional statements:
<txp:if_article_list>
  <txp:if_category>
  <txp:else />
  </txp:if_category>
</txp:if_article_list>
<txp:if_individual_article>
</txp:if_individual_article>

Of course, if you have any Textpattern or HTML code that applies to both the category and non–category versions of the landing page, it can be placed outside the category conditional statements:
<txp:if_article_list>
  <txp:output_form form="meta_data">
  <txp:if_category>
  <txp:else />
  </txp:if_category>
  <txp:output_form form="nav">
  <txp:output_form form="footer">
</txp:if_article_list>

Or like this:
<txp:output_form form="meta_data">
<txp:if_article_list>
  <txp:if_category>
  <txp:else />
  </txp:if_category>
</txp:if_article_list>
<txp:if_individual_article>
</txp:if_individual_article>
<txp:output_form form="nav">
<txp:output_form form="footer">

Category Tags

The conditional statement tags are the same whether you are using Textpattern’s two categories or the rss_unlimited_categories plug–in. The category–specific code, however, may differ. The various category tags that apply to the default installation are given on the Textpattern tag website. The options for the plug–in are listed here.

[This tutorial is part 7 of a series intended to introduce the Textpattern content management system to web designers who have no knowledge of PHP or databases.]