0

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..

1

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.