Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2014-03-10 12:22:51

a7atylor
New Member
Registered: 2012-05-21
Posts: 6

Exclude articles in article_category

I have a next button that takes you to a random article in the same category, the category is chosen in a filter on the side and added to the URL (e.g – /?c=computing). The code so far:

<txp:article_custom category=’<txp:category />’ limit=“1” sort=“rand()”> <a href=’<txp:site_url /><txp:section />/<txp:article_url_title />/?c=<txp:category />’> Next </a> </txp:article_custom>

Problem is this, the current article is not excluded, so potentially the same article could be the next random article. Ive looked at related_articles which would allow me to exclude the current article but would lose the ability I have to filter by the chosen category (e.g – /?c=computing) and the article could be matched in either cat1 or cat2. I also need to keep the category on the end of the url, like it does in the above code.

Any ideas? Ideally I’d want to avoid using a plugin, but I’d consider any solution!

Offline

#2 2014-03-10 15:00:56

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,315

Re: Exclude articles in article_category

Not tested but should be viable at least with minimal alterations:

Inside your current article’s form, get the current article’s ID and save it in a variable.

<txp:variable name="current_article_id" value='<txp:article_id />' />

Use the current ID to find different ones (further notes inside HTML comments):

<!-- Take two random articles IDs, filter against the current one -->
<txp:article_custom category='<txp:category />' limit="2" sort="rand()">
  <txp:if_article_id id='<txp:variable name="current_article_id" />'>  <!-- Do nothing here, it's the current article -->
  <txp:else />  
    <!-- Take the ID if it's different from the current one -->
    <txp:variable name="right_one" value='<txp:article_id />' />	
  </txp:if_article_id>
</txp:article_custom>

<!-- Feed the correct ID into another article_custom tag, here: your link creating tags
        Any attributes other than ID not required -->
<txp:article_custom id='<txp:variable name="right_one" />'>
  <a href='<txp:site_url /><txp:section />/<txp:article_url_title />/?c=<txp:category />'>
    Next				
  </a>		
</txp:article_custom>

Edit: This wouldn’t save visitors, of course, from infinitely looping through article A <—> article B. You’d need cookies/local storage/URL values for doing that sort.


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#3 2014-03-10 15:37:36

a7atylor
New Member
Registered: 2012-05-21
Posts: 6

Re: Exclude articles in article_category

Ah, very clever idea using limit=“2” with an if statement, minimal as well. I like it!

Ive just been trying a more complicated idea of pushing the category’s article id’s in to a variable with article_custom with the aim of excluding the current article using a variable created prior to the article list, but your idea works nicely and uses less resources too.

To extend this (at a later date) I might look at initially generating a random sequence of all article id’s in the chosen category, store them in a variable, and ask the next button to work through the list sequentially until the end, so no article is ever repeated.

Thanks uli!

Offline

#4 2014-03-10 16:09:58

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,315

Re: Exclude articles in article_category

You’re welcome :)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

Board footer

Powered by FluxBB