Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 Yesterday 12:00:04
- Adams
- Plugin Author
- Registered: 2024-08-30
- Posts: 69
Show an article for a specific time period.
Hi,
Is there a way to show a specific article for a specific time period only?
For example, show article 1 when the date is between 5/4/2026 and 5/10/2026.
Else, show article 2 if the date not between the above time periods.
Thank you
Offline
Re: Show an article for a specific time period.
Would it work for you set article 1 to publish on 5 April and to expire on 5 October? If you then set a variable inside that article, you could test whether it exists and output article 2 in its place.
For example:
<!-- article 1 -->
<txp:article_custom id="1" form="my_article_form" />
<txp:if_variable name="feature_exists" not>
<!-- article 2 -->
<txp:article_custom id="2" form="my_article_form" />
</txp:if_variable>
and inside article 1, you would put this inside your article body:
<txp:variable name="feature_exists" value="yes" />
The logic:
- If article 1 is output within its valid dates, it sets the
feature_existsvariable. If that is set, article 2 won’t output. - When article 1 expires, the
feature_existsvariable is not set, so article 2 is output.
You can, of course, use any variable name you like and vary the dates by setting the publish and expire dates in article 1
TXP Builders – finely-crafted code, design and txp
Offline
#3 Yesterday 12:12:39
- Adams
- Plugin Author
- Registered: 2024-08-30
- Posts: 69
Re: Show an article for a specific time period.
I apologize, I forgot to say I’m using custom article in a page that has only one article.
Only I need to replace that article
in my page:
header
<txp:article_custom id=“2” >
<h1><txp:title/></h1>
<txp:body/>
</txp:article_custom>
footer
Offline
Re: Show an article for a specific time period.
Sure, the same principle applies.
I don’t think it matters whether you use article_custom as a container tag like you show or put the article output in a separate form. You’d just have to write it out twice. In fact, you could probably incorporate the variable here instead of in the article body. Once article ID# has expired, the code block won’t output:
<!-- article 1 -->
<txp:article_custom id="1">
<txp:title wraptag="h1" />
<txp:body />
<txp:variable name="feature_exists" value="yes" />
</txp:article_custom>
<txp:if_variable name="feature_exists" not>
<!-- article 2 -->
<txp:article_custom id="2">
<txp:title wraptag="h1" />
<txp:body />
</txp:article_custom>
</txp:if_variable>
TXP Builders – finely-crafted code, design and txp
Offline
#5 Yesterday 12:19:02
- Adams
- Plugin Author
- Registered: 2024-08-30
- Posts: 69
Re: Show an article for a specific time period.
Thank you,
I will try that,
Offline
Re: Show an article for a specific time period.
The above will work great. Really clever idea.
Although more general in nature, there are also some core tools at your disposal if you choose to use the expiry feature. <txp:if_expired> will allow you to wrap your article tag and use <txp:else /> to output something different.
Alternatively, you can use <txp:date> to fashion an expiry date and plug it into your article tag’s month or time attributes, or just use them directly to only display the article from month='<txp:posted />' time="2026-10-05".
Failing all that, you can use <txp:evaluate> to calculate if the current date is less than the article’s posted date + 6 months, and display the article if so, otherwise display a different article or message.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
#7 Yesterday 12:32:25
- Adams
- Plugin Author
- Registered: 2024-08-30
- Posts: 69
Re: Show an article for a specific time period.
Thank you for your help,
Jakob’s code worked, the article #1 showed, indeed, but it never went away after the article expired. I had to go back and change the date and set expiration to now; article #1 disappeared, and original article 2 showed up.
Looks like it’s ignoring the article expiration date and time.
can we mix <txp:if_expired> and Jakob code ?
Last edited by Adams (Yesterday 13:18:43)
Offline
Re: Show an article for a specific time period.
There could be multiple reasons. There’s a setting to publish expired articles, which might be switched on. Try adding expired="0" to the first article_custom tag as described in the article_custom docs. Does that help?
I had to go back and change the date and set expiration to now
This would suggest that an expired article is in fact being recognized as expired. Maybe there’s a problem with a date / time mismatch? There are timezone and summer time Daylight Saving Time options under Admin › Settings. Try experimenting with that? Some people have reported that time in textpattern can be off by an hour (something to do with server and database times?). That might explain why it does work when you set the expiry date manually.
EDIT: There’s an (undocumented) tag called txp:date that you can use to output the current time. You could try temporarily inserting it and seeing if it matches your current time:
<txp:date time="now" calendar="hebrew" format="%H:%M" />
I think the server time is shown under Admin › Diagnostics too. See what happens if you set expiry back or forward by the time difference. You can then at least test whether the expiry-principle works.
Otherwise, I’m not aware that using id="123" would override the time publishing status but I could be wrong. What does the tag trace say (switch on “debug” mode and look at the page source. It should show you the match)?
But if using id="123" does override the normal publishing times status, combining the tags could work like this (untried):
<txp:article_custom id="1">
<txp:if_expired not>
<!-- rest of content -->
</txp:if_expired>
</txp:article_custom>
TXP Builders – finely-crafted code, design and txp
Offline
#9 Today 12:33:49
- Adams
- Plugin Author
- Registered: 2024-08-30
- Posts: 69
Re: Show an article for a specific time period.
Thank you both.
The issue was on publishing expired articles (it was yes). I didn’t know that.
Thank you again for all your helps and support!
Offline