Display conditional block if a post date is older than one year

I have a document Library CPT , and want to show conditional contnent if the post date of a post in the CPT is older than one year ago

Hey Casper,

you could create a small helper function to check if the post is older than a year and use this function in your conditional logic with the dynamic {echo:...} tag.

The function could look like this:

function too_old() {
    global $post;
    // Get the post creation date
    $creation_date = get_the_date( 'Y-m-d', $post );

    // Convert the creation date to a DateTime object
    $creation_datetime = DateTime::createFromFormat('Y-m-d', $creation_date);

    // Get the current date
    $current_datetime = new DateTime();
    // Calculate the difference between the current date and the creation date
    $interval = $current_datetime->diff( $creation_datetime );

    return $interval->days > 365;

You can then use {echo:too_old} within your conditional logic:

CleanShot 2023-05-24 at 15.48.08

See screencast demonstrating the usage.

Let me know if that helped.



thank you so much Andre… works perfectly :smiley: