Tue 26th Sep 2006
Block Comments after X days
Posted in the evening, filed under Blogsome , Smarty Templates.Eugenia (who used to write for BeOS sites, back in the day) wanted to know how to stop comments from being posted on posts that are over 10 days. I’ll generalize this to X days, just for completeness.
You need to get the date of the post, and the current date. I’ll get the post date, and put it into a variable:
{capture name=pdate}{the_time d=“U”}{/capture}
{$smarty.now}
Now, you need to subtract the two of these:
{$smarty.now - $smarty.capture.pdate}
Note that it will be somewhat incorrect, as {$smarty.now} is server time, whilst {the_time} is WordPress time.
Now, this gives us a value in seconds, so we want to work out how long X days is in seconds. This, again is simple:
X days = 60*60*24 seconds
∴ X days = 86400 seconds
{capture name=diff}{$smarty.now-$smarty.capture.pdate}{/capture}
Now, the actual code to display the error message is:
{if $smarty.capture.diff/86400 > 10}
I'm sorry, Comments are now closed.
{/if}
But, we want this to appear instead of a comment form, so you’ll need to find the line that looks a bit like:
{if 'open' == $post->comment_status}
And replace it with:
{if $smarty.capture.diff/86400 > 10}
I'm sorry, Comments are now closed.
{elseif 'open' == $post->comment_status}
Naturally, replace 10 with however many days of comments you wish to allow.
Electrocution (Hydro Mix) • Decoder Ring • Somersault ★★½
Hi, I tried the code like this, and it doesn’t work:
{capture name=pdate}{the_time d=”U”}{/capture}
{capture name=diff}{$smarty.now-$smarty.capture.pdate}{/capture}
{if $diff/86400 > 10}
I’m sorry, Comments are now closed.
{elseif ‘open’ == $post->comment_status}
(the form code follows)
When I try {$diff/86400} the result is always 0. Did I miss something from your instructions? I don’t think that the actual “X days = 60*60*24 seconds ∴ X days = 86400 seconds” is part of the smarty code, right?
Thanks.
7 hours, 26 minutes after the fact.
The problem is that the $diff is simply not getting captured by the system! I tried the {$smarty.now-$smarty.capture.pdate} alone, and that works. But when you try to capture that subtraction inside $diff, $diff stays empty! Any ideas?
7 hours, 42 minutes after the fact.
Ok, I fixed it, it now works. I had to change the variable that you are reffering to as $diff, to $smarty.capture.diff
Thanks!
8 hours, 4 minutes after the fact.
My bad, I’ll fix that up. Thanks.
19 hours, 2 minutes after the fact.