Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
[Solved] Ugly code: how to improve it?
Here is my ugly code. Is there a better way?
<txp:article_custom section='<txp:section />' time="future" limit="1" sort="LastMod desc"><txp:php>
/*
* Compare a future last modified (5 days before) article to
* current date, display a message if the result is true
*/
$post = posted(array('format'=> '%s'));
echo( (strtotime(date('Y-m-d', strtotime('-5 days', $post))) <= safe_strftime("%s") ? '<p class="mt2em txt-c fs120% #a00" id="limited"> Hurry Up!</p>' : '') );</txp:php>
</txp:article_custom>
Last edited by Pat64 (2023-02-23 10:45:35)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: [Solved] Ugly code: how to improve it?
Try this:
<txp:if_expired date="modified" time="+5 days">
<p class="mt2em txt-c fs120% #a00" id="limited"> Hurry Up!</p>
</txp:if_expired>
Offline
Re: [Solved] Ugly code: how to improve it?
…That’s not an expired article, but posted into the future… To be displayed during 5 days before the published date…
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: [Solved] Ugly code: how to improve it?
… Sorry… That’s seems to work (I need to test more). But only in individual article context.
Okay. Here is the final code for both article list and individual context:
<txp:variable name="limited" trim output>
<txp:article_custom section="articles" limit="1">
<txp:if_expired date="modified" time="+5 days">
<p>Hurry !!</p>
</txp:if_expired>
</txp:article_custom>
</txp:variable>
Thanks lot Oleg ;)
It’s definitively obvious that only Devs are the best Textpattern users.
Last edited by Pat64 (2023-02-23 10:46:00)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: [Solved] Ugly code: how to improve it?
Pat64 wrote #334723:
…That’s not an expired article, but posted into the future…
This makes me think that time
should rather be relative to date
than to the current time. Would this look more natural for what you need?
<!-- subtract 5 days from LastMod and compare with the current time -->
<txp:if_expired date="modified" time="-5 days" />
Since you must be the only user of this 4.8.8-feature atm, we can yet change it if you agree.
Offline
Re: [Solved] Ugly code: how to improve it?
Maybe the posted
tag needs a time
attribute to allow such a filter…
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: [Solved] Ugly code: how to improve it?
Pat64 wrote #334962:
Maybe the
posted
tag needs atime
attribute to allow such a filter…
Not sure to understand. Per se, <txp:posted />
does not filter articles, it just outputs their timestamps.
BTW, <txp:posted />
has time
attribute in dev:
<txp:posted time="posted" /> <!-- default -->
<txp:posted time="modified" /> <!-- LastMod -->
<txp:posted time="next Friday" /> <!-- and so on -->
Offline
Re: [Solved] Ugly code: how to improve it?
etc wrote #334970:
BTW, <txp:posted />
has time
attribute in dev:
<txp:posted time="posted" /> <!-- default -->...
<txp:posted time="modified" /> <!-- LastMod -->
<txp:posted time="next Friday" /> <!-- and so on -->
Perfect!
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: [Solved] Ugly code: how to improve it?
I would like to know more about <txp:posted time="next Friday" />
as it appears counter intuitive to me.
Does it actually mean that you post an article on a future date which happens to be the same as next Friday’s and will appear from today to next Friday? Also what happens to the visibility of the article on Saturday?
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: [Solved] Ugly code: how to improve it?
… Based on my tests on TXP 4.9-Dev, the time
attribute allows us to display (not affect) a custom date of an article. Useful for some dates comparisons (e. with the evaluate
tag).
Last edited by Pat64 (2023-04-04 01:36:29)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: [Solved] Ugly code: how to improve it?
Well, txp somewhat lacks date tags (how do you output the current date?), so I have abused the internal time
parameter of <txp:posted />
, turning it into attribute. But this does not feel very clean, so a new <txp:date />
tag would be better, perhaps?
Offline
Re: [Solved] Ugly code: how to improve it?
Sure! If you think it’s essential, a <txp:date />
tag could be extremely useful. Sometime, dealing with dates is currently difficult.
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline