If Post Published Date or Modified Date is 1 Year or Older, Display Notice on Post Page

I need a way to inform visitors if the post published date or modified date is older than 1 year and if so, display a message on the post page.

I have added an code to my content-single.php file, but without the desired results in terms of checking against both published and updated time.

Example:

if (strtotime($post-post_date)  strtotime('-1 year')){
    echo 'Old Post';
} else {
    echo 'Not Old Post';
}

Any ideas as I'm in the dark here..

Topic date php date-time posts Wordpress

Category Web


Right now you're just checking for the post date. You'll need to check for 'post_modified' aswell. You can find the complete post-object here: https://developer.wordpress.org/reference/functions/get_post/#user-contributed-notes

You can modify the check to look something like this:

if (strtotime($post->post_date) < strtotime('-1 year') && strtorime($post->post_modified) < strtotime('-1 year')){
    echo 'Old Post';
} else {
    echo 'Not Old Post';
}

That should check for both.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.