Old post notification?

Hey folks,

I’m trying to create a notification where posts over 3 years old display a warning message. I’ve bundled the following together, but it doesn’t seem to be working:

<?php
  $old_post = 60*60*24*1095; // 3 years
    if((date('U')-$page->published()->toDate('U')) > $old_post) {
      echo '<div class="old-notice">This post is more than three years old and may contain incorrect information or <a href="/disclaimer">opinions I no longer hold</a>.</div>';
    }
?>

I’m getting the following error with this snippet:

Whoops \ Exception \ ErrorException (E_USER_DEPRECATED)
Passing a single space as value to `Xml::attr()` has been deprecated. In a future version, passing a single space won't render an empty value anymore but a single space. To render an empty value, please pass an empty string.

The if logic seems to work as this error only displays on posts older than 3 years old.

Any help would be greatly appreciated.

Thanks,

Kev

Hm, the error message doesn’t seem to fit the code snippet you posted.

Could you post a screenshot of the whoops page, please?

On a side note it is not good practice to echo HTML, recommended:

<?php
$old_post = 60*60*24*1095; // 3 years
if((date('U')-$page->published()->toDate('U')) > $old_post): ?>
    <div class="old-notice">This post is more than three years old and may contain incorrect information or <a href="/disclaimer">opinions I no longer hold</a>.</div>
<?php endif ?>

Thanks for the tip - I’ll remove the echo.

So fresh back from a dog walk, I’ve worked out that the issue is with 1 particular post. It was just a coincidence that that particular post is the first one that’s 3 years old or more. If I go to older ones, they display the message just fine.

Weirdly though, the same post on my live site doesn’t display an error. I checked if it was the code I’ve added, but it wasn’t, the post still bugs out locally, even when the new code is commented out. So I assume it’s something to do with the development server?

Here’s the whoops message regardless:

Thanks, Sonja.

Kev

Ok, unfortunately the screenshot is cut off at the bottom where the interesting information is. Seems to be an issue with Html::img() somewhere in your template or in snippets used in your template.

Aaaaand weirdly, I stop the server, restart it again and the error has vanished. :man_shrugging:

Magic :magic_wand: