I need some assistance on creating a conditional statement in my Product Archive.
I would like to display an image if an ACF date field is greater than today.
I have done the following:
-- Confirmed that the ACF date field as stored in the database (hardback_pub_date) is numeric (e.g. 20200315 for March 15, 2020)
-- Created a shortcode to produce a numeric value for today (e.g. 20200217)
-- Registered this shortcode with Toolset and confirmed that produces the numeric field
-- Created the conditional statement in my Product Archive
The relevant code I added is below.
"Now" Shortcode:
function now_func( $atts ){
return date("Ymd");
}
add_shortcode( 'now', 'now_func' );
Conditional statement:
[wpv-conditional if="( $(hardback_pub_date) gte '[now]' ) )"]
<p>test text</p>
<img src='/PATH.png'/>
[/wpv-conditional]
Unfortunately, this is not currently working. Here is what I have done for troubleshooting:
-- Confirmed that the image source is working by adding it to another conditional statement. It displayed without any issue in the working conditional.
-- Changed the conditional from gte to lt (as that would then encompass nearly every product in the shop) and the image and test text did not display.
-- Temporarily removed the other conditionals on the page (which display the "Out of Stock" and "On Sale" images) in case they were conflicting with the new conditional but the image and test text still do not display.
My site is enlace oculto. The password to view the site is "WeLoveBooks".
My shop page URL is enlace oculto. The first product on that page, "Dance, Nana, Dance / Baila, Nana, Baila", has a hardback_pub_date value of 20200324 in the database.
Hello,
I assume the ACF plugin stores custom date field value in string format, for example: 2020-2-19.
You can try to turn the string value into timestamp format, like this:
1) Add below PHP codes in your theme file "functions.php":
add_shortcode( 'str-to-time', function($atts, $content){
$atts = shortcode_atts( array(
'field' => 'hardback_pub_date',
), $atts );
$str = get_post_meta(get_the_ID(), $atts['field'], true);
return strtotime ( $str );
});
2) Dashboard-> Toolset-> Setting-> Front-end Content
in section "Third-party shortcode arguments", add above shortcode name: str-to-time
3) Setup the shortcode like this:
[wpv-conditional if="( '[str-to-time]' >= now() )" debug="true"]
<p>test text</p>
<img src="/PATH.png">
[/wpv-conditional]
More help:
https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/
The value that I see in the database for the ACF custom date fields is in the Ymd format, for example: 20200219 but apparently it doesn't output that way when I call the hardback_pub_date in Toolset.
I went ahead and created another shortcode similar to the one you proposed that returns a Ymd format and then compare that to the [now] shortcode.
My shortcodes (located in functions.php and registered with Toolset at Settings > Front-end Content > Third-party shortcode arguments):
// Hardback Publication Date in Ymd format
add_shortcode( 'hardback-pub-date', 'hardback_pub_date_func' );
function hardback_pub_date_func($atts, $content) {
$atts = shortcode_atts( array(
'field' => 'hardback_pub_date',
), $atts );
$str = get_post_meta(get_the_ID(), $atts['field'], true);
return $str;
}
// Now in Ymd format
add_shortcode( 'now', 'now_func' );
function now_func( $atts ){
return date("Ymd");
}
My conditional statement in the Toolset archive:
[wpv-conditional if="( [hardback-pub-date] gte [now] )" ]
<img src='/PATH.png'/>
[/wpv-conditional]
This is working now. I appreciate your help!
My issue is resolved now. Thank you!