Textpattern tips, tutorials and code snippets

Display sticky articles first in an article list

Here’s another handy and clever way to sort an article listing by using the following criteria:

  1. If one article in the current level, home, category or section has a sticky status, display it first.
  2. Else display the regular live article

If a user chooses the sticky status for an article, then display it first.

Why would you like to do this?

Among the various available options in the article tab this is a very simple solution for the end user and “Sticky” makes sense for a user to glue an article somewhere on a page.

Of course you could also check for the value of a custom field or a category. But this requires addionnal user input. So let’s keep it simple by just switching the status of the article to “Sticky”.

Julián (maniqui) offered a very simple and clever solution:

<txp:variable name="stickit"><txp:article status="sticky" limit="1">dummy text</txp:article></txp:variable>

He creates a variable and tries to fill it with a sticky article and forcing it’s value to “dummy text” or “Cheesecake” or whatever crosses your mind at that moment. Of course this value will never appear, it’s just a flag.

If it’s empty, no sticky article can be found at the current level. If it’s not empty, we have a sticky article.

Here’s the full snippet:

<txp:if_article_list>
<txp:variable name="stickit"><txp:article status="sticky" limit="1">dummy text</txp:article></txp:variable>
	<txp:if_variable name="stickit" value="">
		<!-- There's no sticky article ! Just fire the first live article found -->
			<txp:title />
			<txp:body />
	<txp:else />
		<!-- We have a Sticky article, just display it -->
		<txp:article status="sticky" limit="1">
		<txp:title />
		<txp:body />
		</txp:article>
	</txp:if_variable>
</txp:if_article_list>

Bon appetit!

This tip was covered in the Textpattern forum over here.

Use Textile help to style your comments