Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2007-07-04 15:46:04

sthmtc
Member
From: CGN, GER
Registered: 2005-01-17
Posts: 586
Website

display one random article from the last x most recent articles

how can i display one random article out of the last – let’s say three – articles?
maybe i’m too tired or something like that but i just can’t figure it out…?!

any help is highly appreciated…

Last edited by sthmtc (2007-07-04 15:46:18)

Offline

#2 2007-07-05 04:24:57

wet
Developer Emeritus
From: Schoerfling, Austria
Registered: 2005-06-06
Posts: 3,376
Website GitHub Mastodon

Re: display one random article from the last x most recent articles

I couldn’t think of a method employing just built-in tags, this requirements would probably best be solved by a plugin. GTalk/Skype for details?

Offline

#3 2011-05-18 09:23:27

stickid
Member
From: Bremen, Germany
Registered: 2009-02-14
Posts: 15
Website

Re: display one random article from the last x most recent articles

Hi,

I need the same functionality and have absolutely no idea how to do it.
…I didn´t saw a problem here when I worked on the concept for this project.

Does anyone have an idea how to make it?

Regards and thanks. Nikolaj

Offline

#4 2011-05-18 12:46:31

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,001
Website GitHub

Re: display one random article from the last x most recent articles

Thinking aloud, maybe you could use soo_article_filter for this? The idea would be to prefilter the last three articles with soo_article_filter, then randomly choose one of them with article(_custom) and the attributes limit="1" sort="(rand()".

Another approach, albeit a bit roundabout: generate a random number between one and three and save it in a txp:variable. Then use txp:article(_custom) with a counter to grab your three most recent articles along with a counter. Only output the content when the counter matches the random number:

<!-- put a random number in a variable -->
<txp:variable name="random_choice" value='<txp:php>echo rand(1,3);</txp:php>' />

<!-- initialize the counter variable -->
<txp:variable name="counter" value="0" />

<txp:article_custom section="my-section" limit="3" sort="posted desc">
  <!-- increment the counter variable -->
  <txp:variable name="counter" value='<txp:php>global $variable; $variable['counter'] = intval($variable['counter'])+1;</txp:php>' />
  <!-- show content if the current counter is the same as the predefined random number -->
  <txp:if_variable name="counter" value='<txp:variable name="random_choice" />'>
... your output here ...
  </txp:if_variable>
</txp:article_custom>

If you are allergic to php in txp, you can use rah_function (for the first rand() case) and aks_var or adi_calc or rvm_counter for the counter.

PS: All the above untested. If I’ve got the php wrong, see this thread for running/accessing the txp:variable function in php.


TXP Builders – finely-crafted code, design and txp

Offline

#5 2011-05-18 14:22:09

stickid
Member
From: Bremen, Germany
Registered: 2009-02-14
Posts: 15
Website

Re: display one random article from the last x most recent articles

THANKS. At first glance this is what I need. Thanks alot, I will give you a feedback.

Offline

#6 2011-05-18 17:35:06

michaelkpate
Moderator
From: Avon Park, FL
Registered: 2004-02-24
Posts: 1,379
Website GitHub Mastodon

Re: display one random article from the last x most recent articles

Since it doesn’t require too many logic statements, if you would like to use built-in capabilities as much as possible:

<!-- put a random number in a variable -->
<txp:variable name="random_choice" value='<txp:php>echo rand(1,3);</txp:php>' />

<!-- initialize the counter variable -->
<txp:variable name="counter" value="1" />

<txp:article_custom section="my-section" limit="3" sort="posted desc">
  <!-- show content if the current counter is the same as the predefined random number -->
  <txp:if_variable name="counter" value='<txp:variable name="random_choice" />'>
... your output here ...
  </txp:if_variable>

  <!-- increment the counter variable -->
  <txp:if_variable name="counter" value="2">
      <txp:variable name="counter" value="3" />
  </txp:if_variable>

  <txp:if_variable name="counter" value="1">
      <txp:variable name="counter" value="2" />
  </txp:if_variable>

</txp:article_custom>

Perhaps it is time to ask once again why we don’t have a txp:random tag?

Last edited by michaelkpate (2011-05-18 17:35:43)

Offline

Board footer

Powered by FluxBB