Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
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
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
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
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
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
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