Textpattern tips, tutorials and code snippets

Create next prev links according to category1

This feature is for people who want make a prev/next article link according to the assigned value of category1.

If there are for example 5 articles with category1 asigned “test” and we are viewing the 3rd article, the next/prev link will point only to articles from this category – any other article with another category1 will not appear in the links.

For this to work, you will need Stef Dawson’s plugin smd_if and Jon-Michael Deldin’s jmd_count.

  1. Download the smd_if plugin, install and activate.
  2. Download the jmd_count plugin, install and activate.

With this code you will get something like link_to_next+filter(category1). If one looks deeper into the code, you may notice that we can output any information from next/prev article, because we know their IDs:

For example:

<txp:article_custom id='<txp:variable name="prev_id"/>'><txp:keywords /></txp:article_custom /><br /> 
<txp:article_custom id='<txp:variable name="next_id"/>'><txp:title /></txp:article_custom />

This code will output keywords for prev article and the title of next article. Imagine we have 13 articles:

ID category1
1 cats
2 birds
3 people
4 games
5 birds
6 people
7 cats
8 birds
9 people
10 cats
11 birds
12 people
13 cats

Sorting is made by IDs – the list of IDs of articles with equal category1 is building (category1 is choosen from current article’s category1, for example, we are viewing article #9 and our category is people. We get this IDs: 3, 6, 9, 12. Сurrent article’s ID is 9, then we build links (or anything else) to 6th and 12th articles. If we are looking throught first(last) article, than prev(next) link will be built to last(first) article from list:

More examples of showing link if we are looking last/first article in list:

  • current_id=12, prev_id=9, next_id=3;
  • current_id=3, prev_id=12, next_id=6.

If there are 3 or less articles with current category1, than code will output nothing. To suppress such behaviour, just delete txp:jmd_if_count. 

And as conclusion, here is the code:

<txp:jmd_if_count eval=">3" table="textpattern" where='category1="<txp:category1 />"'>
<txp:smd_if field="category1" operator="isused"><txp:variable name="prev_id" value=""/><txp:variable name="next_id" value=""/>

<txp:variable name="list" value='
<txp:article_custom sort="posted desc" limit="999" category="{smd_if_category1}"><txp:article_id /> </txp:article_custom />
' /> 

<txp:php>
$array=variable(array('name' =>'list'));
$array=explode(" ",trim($array));
$value=$GLOBALS['thisarticle']['thisid'];

$current_key=array_search($value, $array);
$current_key==0 ? $prev_key=count($array)-1 : $prev_key=$current_key-1; // if cur_key is first => prev_id is last item
$current_key==(count($array)-1) ? $next_key=0 : $next_key=$current_key+1; // if cur_key is last => next_id is first item

variable(array('name' =>'prev_id', 'value' => $array[$prev_key]));
variable(array('name' =>'next_id', 'value' => $array[$next_key]));

//echo "${array[$prev_key]} +${array[$current_key]}+ ${array[$next_key]}";
</txp:php>

Next article: <txp:article_custom id='<txp:variable name="prev_id"/>'><txp:permlink><txp:title /></txp:permlink></txp:article_custom /><br />
Previous article: <txp:article_custom id='<txp:variable name="next_id"/>'><txp:permlink><txp:title /></txp:permlink></txp:article_custom />

</txp:smd_if>
</txp:jmd_if_count>

1 Comment Comment feed

Hi & thank you very much for your contribution.

I have changed the category1 you used & replaced it with a custom_field that I am using. I get all the right articles being listed as I would expect, which is great!

However I get a complete list of articles, rather than just 1 “next” & 1 “previous” article title being displayed. Any idea why that is? Can I use limit=“1” to reduce the list to a single preceding & next article?

Also, both the “next” & 1 “previous” article lists are in ascending order. Shouldn’t one of them (the previous list) be in descending order? I noticed that you used <txp:article_custom sort=“posted desc”……> but the descending attribute doesn’t work.
Have you got any ideas how I might get your code to work for me?

Thanks & I look forward to hearing from you soon,
Martin (UK)

Add a comment

Use Textile help to style your comments