Lab 99 Web Design

Skip the navigation

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:

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:

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 New category 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, an excellent plug-in has been developed which allows the creation of an unlimited number of categories. For various reasons, however, the rss_unlimited_categories plug-in has not been fully maintained. At the time of writing, the latest version of the plug-in (version 0.7.6.6-k) still works with the latest version of Textpattern (version 4.8.8), but only when using PHP version 7; it is incompatible with PHP 8. Whether it will work with future versions of Textpattern is uncertain. This version of the plug-in can be:

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 Textpattern page.

We have seen in the Articles and Sections tutorial how to change the default settings so that the landing page displays a limited number of articles from its own section, or displays articles from another section.

You can also change the default settings so that the HTML 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:

Landing Page by Category Only

If you want the HTML landing page 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 HTML pages, you will need to surround the code for the landing page with the standard conditional statements which we used in the previous tutorial:
<txp:if_article_list>
   <txp:if_category>
      <!-- category-specific landing page code goes here -->
   <txp:else />
      <!-- non-category-specific landing page code here -->
   </txp:if_category>
</txp:if_article_list>
<txp:if_individual_article>
      <!-- individual article code goes here -->
</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">
   <body>
      <txp:output_form form="nav">
      <txp:if_category>
         <!-- category-specific landing page code goes here -->
      <txp:else />
         <!-- non-category-specific landing page code here -->
      </txp:if_category>
      <txp:output_form form="footer">
   </body>
</txp:if_article_list>

Or like this, if we include the individual-article code:
<txp:output_form form="meta_data">
<body>
   <txp:output_form form="nav">
   <txp:if_article_list>
      <txp:if_category>
         <!-- category-specific landing page code goes here -->
      <txp:else />
         <!-- non-category-specific landing page code here -->
      </txp:if_category>
   </txp:if_article_list>
   <txp:if_individual_article>
      <!-- individual article code goes here -->
   </txp:if_individual_article>
   <txp:output_form form="footer">
</body>

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 can be found at https://docs.textpattern.com/tags/index. The options for the plug-in can be found by going to Admin then Plugins and, once you have installed the plug-in, clicking its Help link:

Textpattern plug-in screen

And that’s it! Thank you for reading!

[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.]