Custom navigation axes

2012-10-11 13:59

Keywords: navigation, next, previous, links, sort, order

A popular question on Textpattern Forum is whether <txp:link_to_prev/next /> tags respect articles sort attribute. They do now (v. 4.7), but not for complex sort orders, and not at all with <txp:article_custom />. You can however easily simulate it with etc_query. All you have to do is create an article list, target the current article and display its neighbors. Let us see how to.

Suppose we would like to create prev/next_by_category2 links. Firstly, we generate a Category2-sorted article list and store it in some <txp:variable />:

<txp:variable name="category2-list">
  <txp:article_custom limit="999" sort="Category2">
    <a href="<txp:permlink />"><txp:title /></a>
  </txp:article_custom>
</txp:variable>

Since this is the most time-consuming part, you might consider wrapping <txp:article_custom /> in etc_cache. Now we call etc_query to construct the links:

<txp:etc_query
  data='<txp:variable name="category2-list" />'
  query='a[@href=''<txp:permlink />'']'>
    {preceding-sibling::a[1]@@?=&#171; Previous by category: {?}@rel=prev}
    {following-sibling::a[1]@@?=Next by category: {?} &#187;@rel=next}
</txp:etc_query>

That’s all, really. If you wish, you can add in the same manner another navigation list, say by Title. Below is the result:

File(s)