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 626 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
Checking if the current Page/Post has Children i.e. is a Parent in Bricks

Checking if the current Page/Post has Children i.e. is a Parent in Bricks

Shows how we can check whether the current Page or Post (of any hierarchical post type) is a parent
Categories:
Pro
Check If The Latest Post Was Published in The Last X Minutes

Check If The Latest Post Was Published in The Last X Minutes

In this Pro tutorial we shall define a custom function that takes in the number of minutes as an argument and returns true if there…
Categories:
Conditionally Hide a Section on a Specific Taxonomy’s Term Archives in Bricks

Conditionally Hide a Section on a Specific Taxonomy’s Term Archives in Bricks

A user asked: I have a CTA in the footer of my site, which I have custom fields on various templates, etc to populate. I…
Categories:
Pro
ACF Repeater sub field value dynamic data condition in Bricks

ACF Repeater sub field value dynamic data condition in Bricks

This Pro tutorial provides the steps to output an element on single posts (can be of any post type) only if a specified ACF (Pro)…
Create A Customizable AJAX Add To Cart Button In Bricks Builder

Create A Customizable AJAX Add To Cart Button In Bricks Builder

In Bricks, you can simply create an Add To Cart button from a dynamic data / function {woo_add_to_cart}. This button supports AJAX as well if…
Bricks Condition for WishList Member

Bricks Condition for WishList Member

How to render a Bricks element only if the user has access to a specified WishList Member level.
Categories:
Pro
Condition to Check if the Current Category Has At Least One Child

Condition to Check if the Current Category Has At Least One Child

Looking to render an element in the category archive Bricks template only if the category of the current category archive page is a parent? This…
Categories:
Pro
Conditional Rendering in Bricks Based on CSS Class

Conditional Rendering in Bricks Based on CSS Class

Updated on 19 Jun 2024 A user asks: Is there a way to apply the same conditions to several elements like we set styles appending…
Categories: