Skip Navigation

[Resolved] Display element conditionally depending on post recency

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by szymonF 1 year, 10 months ago.

Assisted by: Waqar.

Author
Posts
#2406237

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?

#2406765

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

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

#2406885

This worked a dream, super. Thank you for a very clear and to-the-point explanation!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.