Simple Archives | Coding | the bombsite

. . . . . . . . . . .

Simple Archives

With the advent of version 4.0.2 of Textpattern came several new tags, one of which is <txp:if_different>. It doesn’t sound very exciting really does it? Well actually it is an extremely flexible and powerful tag and I’m going to show you how to produce an archive by category with a menu and an archive by month with a menu using this tag and no plug-ins whatsoever! This is an expansion of Zem’s original forum post.

Category Archives Menu

For my purposes I created a new section called “archive” and a new template specifically for this section. You could re-use an existing template if you wished using the <txp:if_section> tag to keep the code separate. It’s up to you but you do need to create a new section.

First, you will see in my archives that I start with a category menu. This is auto-generated using a single tag and a Form template. The titles in the menu have anchor tags in them so that they link to the appropriate category listing. The menu is a <ul> with each entry having it’s own <li> so the code for the page template looks like this:-

<ul class="className">
<txp:article_custom form="catmenu" limit="9999" section="article" sort="Category1 asc" />
</ul>

All the articles in my archive are from a specific section so I name this with the “section” attribute. You may want articles from all your sections in which case you can leave this out. I want the list sorted alphabetically by category name starting with “A” at the top which is what the “sort” attribute is for. The tag is wrapped with <ul> tags as the output from the form is a list of <li> tags.

I then created a new “article” type Form template called “catmenu” which the above tag calls and used this magical piece of code in it:-

<txp:if_different>
<li><a href="#<txp:category1 />"><txp:category1 title="1" /></a></li>
</txp:if_different>

What this says is that if it is different output the code in the middle. That code is outputting the category1 title wrapped in an anchor link which is the category1 name and the whole is wrapped in <li> tags. So basically, if the category changes, output the new category title with an anchor wrapped in an <li>. That’s the menu sorted. Simple really isn’t it?

Category Archives

The category listing itself is generated in the same manner, using a single <txp:article_custom /> in the page template and a single FORM template.

In your page template you need to use the tag like this:-

<txp:article_custom form="categories" limit="9999" section="article" sort="Category1 asc,Posted desc" />

The tag will use the categories Form template which I shall describe below. I only want to list articles from my article section so that attribute is specified. You may want articles from all your sections in which case you can leave this out. The “sort” attribute will first sort by “Category1” to split out the categories then by “Posted” to put the articles into date order. The “asc” sorts the categories alphabetically then “desc” places the newest article at the top of each list.

So now for the form template and the TXP voodoo. Our first sort criteria is Category1 so we want to know when the category changes so that we can output a category title, then the second sort criteria takes over and sorts the articles by date. This is how the form looks:-

<txp:if_different>
<a name="<txp:category1 />" id="<txp:category1 />"></a><div class="className"><a href="<txp:site_url />article/?c=<txp:category1 />"><txp:category1 title="1" /></a></div>
</txp:if_different>
<h3 class="className"><span class="className"><txp:permlink><txp:title /></txp:permlink></span><span class="className"><txp:posted /></span></h3>

And that’s all there is to it.

So what is it doing? Well the first part says if different output a linked category title which means that each time there is a new category it outputs a linked title. Then underneath that it outputs the linked article titles and dates for that category in date order each contained in an <h3> until it meets a new category when it outputs the new linked category title and so on and so forth till it runs out of articles.

Now I said earlier that I was generating the anchor tags automatically and that is this bit of code:-

<a name="<txp:category1 />" id="<txp:category1 />"></a>

The code for the linked title is complicated by a method I use for sending a category listing to the section, and therefore the page template, that is already being used to display the articles instead of the default template that TXP normally uses. My articles are from the article section and as the page template for that section already contains the perfect code for displaying the articles why use something else? The specific piece of code that is doing this is:-

<a href="<txp:site_url />article/?c=<txp:category1 />"></a>

which is wrapped around the tag that outputs the category title:-

<txp:category1 title="1" />

If you want to keep TXP’s default behaviour for category lists, which might be advisable if your articles are from various sections, remove the extra coding and automatically link the category title so the code would look like this:-

<txp:if_different>
<a name="<txp:category1 />" id="<txp:category1 />"></a><div class="className"><txp:category1 title="1" link="1" /></div>
</txp:if_different>
<h3 class="className"><span class="className"><txp:permlink><txp:title /></txp:permlink></span><span class="className"><txp:posted /></span></h3>

Monthly Archives

That is basically it but you can use this same method to output an archive by month listing as well.

I no longer use Monthly Archives but this is the method I used when I did.

I created another new section called archivemonthly and used the same template as the category archives using <txp:if_section> tags to separate the code.

The Menu

The following code will allow you to have a menu at the top listing the years which allows you to go directly to all the listed articles for that year. This could be expanded to show months as well if you wish using an extra <txp:if_different> block shown further down in “The Archives” (with appropriate HTML tags). The method used is similar to the category menu. I already have the form called “catmenu” which can be used for both menus by using <txp:if_section> tags to separate the 2 pieces of code. For this particular menu the code looks like this:-

<txp:if_different>
<li><a href="#Y<txp:posted format="%Y" />"><txp:posted format="%Y" /></a></li>
</txp:if_different>

As you can see it is very similar to the category menu in method but is now looking for changes in the year taken from the articles’ “posted” dates. The anchor tag also picks up the year so that we can create the link to the appropriate part of the listing.

In your page template you call this code using:-

<ul class="className">
<txp:article_custom form="catmenu" limit="9999" section="article" />
</ul>

I’m still calling my “article” section but if you have articles in several sections you can ignore this. As the list is date-related there is no need for the “sort” attribute as article are sorted by date by default. The combination of page template and form template tags results in an unordered list which you can style your way. Do remember to replace all instances of “className” with a name of your choosing.

The Archives

This is the tag you will need for your page template:-

<txp:article_custom section="article" limit=99999 form="monthly_article" />

Still calling the article section though that isn’t needed if you have several sections and there is no need for the “sort” attribute here either. This time we are calling a FORM template called “monthly_article” which is where the magic is performed. This is how it would look:-

<!-- show the year -->
<txp:if_different>
<a name="Y<txp:posted format="%Y" />" id="Y<txp:posted format="%Y" />"></a><h2 class="className"><txp:posted format="%Y" /></h2>
</txp:if_different>
<!-- show the month -->
<txp:if_different>
<h3 class="className"><txp:posted format="%B" /></h3>
</txp:if_different>
<!-- article title and link -->
<txp:permlink><txp:title /></txp:permlink> (optional <txp:posted /> )
<br />

You can see that there are 2 blocks of <txp:if_different> here. The first is checking the posted date for a change of year and when it changes it outputs an <h2> containing the year and an anchor tag for the menu link, then the second block checks for a change of month and if that changes it outputs an <h3> containing the month. Then the last bit outputs a list of linked article titles until either the month or the year changes at which point it outputs another <h2> or <h3> or both and so on and so forth until it runs out of articles again. You can add “posted” to the output if you require it, but as the listing is already split into years then months it seems a bit superfluous to me unless it is an events listing or something.

Talking of events listings you will probably want to create a listing which requires that you call future events so here is another little trick for you. In the <txp:article_custom /> tag above used for the page template you can add the attribute time=“future” which will restrict the output to events from today onwards allowing you to create an events planner type of output.

There you are then. Archives by category and by date, a couple of linked menus and a future events listing thrown in for good measure and not a single plug-in in sight. Lovely.

Comments ( 3 )

Stuart, the “The Bomb Site Category Archives” link points to http://localhost/thebombsite/archivescat.

2 June 2008, 22:14

Thanks Matt. Corrected.

3 June 2008, 23:14
Phil Birnie

Thanks Stuart — this was a helpful resource for me. Well explained.

17 September 2010, 22:08

Commenting is closed for this article.