I want to display a 'NEW' graphic on a custom post type. I would like to automate it so that the graphic displays conditionally whenever the post date is within the last 30 days. I can see lots of conditional display conditions, but post date is not there. Can you let me know if this can be done?
Hi,
Thank you for contacting us and I'd be happy to assist.
To achieve this, you'll first need to register a custom shortcode, that can return the number of days, lapsed between the post's published date and today:
add_shortcode('calc_diff_in_days', 'calc_diff_in_days_func');
function calc_diff_in_days_func() {
$now = time();
$pub_date = do_shortcode('[wpv-post-date format="U"]');
$datediff = $now - $pub_date;
return round($datediff / (60 * 60 * 24));
}
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.
Next, please add "calc_diff_in_days" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.
After that, in your content template of view, you can use this shortcode in a conditional statement, like this:
[wpv-conditional if="( '[calc_diff_in_days]' <= '30' )"]
New
[/wpv-conditional]
As a result, the "New" text will only show if the post is published within the last 30 days.
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
This worked a dream, super. Thank you for a very clear and to-the-point explanation!