28th Sep '24
/
0 comments

Checking if the current post has been published within the last x days

Checking if the current post has been published within the last x days

Looking to conditionally output an element if the current post (either in a query loop or when viewing a single post) has been published in the last x number of days?

Here’s how.

Step 1

Register a custom function with the number of days as a parameter and returns true or false depending on whether the current post is written in those number of days.

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

<?php

/**
 * Check if the current post is published within the last X days.
 *
 * @param int $days Number of days to check against. Default is 30.
 * @return bool True if the post is published within the specified number of days, false otherwise.
 */
function bl_is_post_published_within_days( int $days = 30 ): bool {
    $current_time = time();
    $post_time    = get_the_date( 'U' );
    $time_diff    = $current_time - $post_time;
    $days_in_seconds = $days * 24 * 60 * 60;

    return $time_diff < $days_in_seconds;
}

We are setting the default function argument value to be 30 days.

Step 2

The function can now be used like this:

if ( bl_is_post_published_within_days() )

for 30 days

or

if ( bl_is_post_published_within_days( 7 ) )

for 7 days

If you are using Bricks make sure you whitelist the bl_is_post_published_within_days function.

Ex.:

<?php 

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'bl_is_post_published_within_days'
  ];
} );

You should also add other functions (native or custom) being used in your Bricks instance besides bl_is_post_published_within_days. This can be checked at Bricks → Settings → Custom code by clicking the Code review button.

More info on whitelisting can be found here.

Then set up a dynamic data condition like this:

{echo:bl_is_post_published_within_days}

If you want to check for number of days other than 30, specify it as an argument like this:

{echo:bl_is_post_published_within_days(7)}

Reference

https://brickslabs.com/new-ribbon-for-posts-in-bricks/

Get access to all 610 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
Day of Week Condition in Bricks

Day of Week Condition in Bricks

Displaying elements conditionally based on the current day of the week.
Categories:
Pro
Conditional Output in Bricks based on if Content has Headings

Conditional Output in Bricks based on if Content has Headings

How to prevent the output of the table of contents if the content has no headings.
Categories:
Tags:
Conditionally Outputting Based on Query Count in Bricks

Conditionally Outputting Based on Query Count in Bricks

Update on 22 Sep 2023: There's a much simpler built-in method now. See this post. In the Bricks Facebook group a user asks: Is there…
Categories:
JetEngine Checkbox Condition in Bricks

JetEngine Checkbox Condition in Bricks

How elements in Bricks can be conditionally output depending on the selected option from a JetEngine checkbox field.
Categories:
Tags:
Pro
Conditional single post Bricks template based on internal referrer URL

Conditional single post Bricks template based on internal referrer URL

How to automatically select a single post template depending on the page from which the post is accessed.
Pro
Conditional Rendering Based On Meta Box Group’s Sub Field

Conditional Rendering Based On Meta Box Group’s Sub Field

This Pro tutorial shows how we can output an element on a single post pages only if a specified sub-field (custom field created using Meta…