Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Using article_custom to pick a single random article in the last year
I have a “Featured Article” sidebar, which is currently just picking a random article from my entire blog. What I’d like to do is make article_custom pick one random article from only those that were published in the last 365 days. Is this possible? I guess as a last resort I was thinking of using something like smd_query. I saw this post that seemed related, but the solutions don’t happen to work here, because I need to get a list of articles from the last year first, then just pick one from that list randomly. Otherwise I might not get any result.
Last edited by maruchan (2013-12-31 22:14:53)
Offline
#2 2014-01-01 01:22:14
- GugUser
- Member
- From: Quito (Ecuador)
- Registered: 2007-12-16
- Posts: 1,473
Re: Using article_custom to pick a single random article in the last year
<txp:article_custom section="blog" month="2013" limit="1" sort="rand()" form="your-form" />
Offline
Re: Using article_custom to pick a single random article in the last year
Thanks GugUser, I thought about using that, too. But for example, I’d like to include articles from January 2014 if the current month is February 2014.
Offline
Re: Using article_custom to pick a single random article in the last year
OK, here is my solution using smd_query:
<txp:smd_query query="SELECT Title, Image, url_title FROM txp_textpattern WHERE Section='articles' AND `Posted` >= CURDATE() - INTERVAL 1 YEAR AND status=4 ORDER BY RAND() LIMIT 1">
<div class="sidebar-article-image">
<txp:images id="{Image}">
<a href="<txp:site_url />articles/{url_title}?ref=sidebar-gallery-imglink" title="Read the article">
<img src="<txp:site_url />timthumb.php?src=<txp:image_url />&w=260" alt="{Title} image" />
</a>
</txp:images>
<h1>{Title}</h1>
<p><a href="<txp:site_url />articles/{url_title}?ref=sidebar-gallery-textlink">Read the article now.</a></p>
</div> <!-- sidebar article image -->
</txp:smd_query>
Edit: The only thing I’m not sure about is what to do about articles that may be missing images. In this case, none of them are, but I might use smd_if or adi_variables or something like that to do some testing inside the query tags.
Last edited by maruchan (2014-01-01 05:46:19)
Offline