I am trying to: use an email address from a types field (for an event CPT) to make the event's speaker's name a link to their email - but only if the email field is not empty.
[wpv-conditional if="( NOT(empty($(wpcf-speakers-email))) )"]<a href="mailto:[types field='speakers-email' raw='true'][/types]?subject=Inquiry from CNSV talk">[types field="name-of-speaker"][/types]</a>[/wpv-conditional]
I cannot get this to display correctly using wpv-conditional; however, it does work correctly using wpv-if.
Is there a better/easier way to do this?
Thanks,
Jeff Safire
--------------
Hi, you can try testing for a non-empty string like this:
[wpv-conditional if="( $(wpcf-speakers-email) ne '' )"]<a href="mailto:[types field='speakers-email' raw='true'][/types]?subject=Inquiry from CNSV talk">[types field="name-of-speaker"][/types]</a>[/wpv-conditional]
If that doesn't work, add the debug="true" attribute to the wpv-conditional shortcode like this, and let me know what you see on the front-end of the site:
[wpv-conditional if="( $(wpcf-speakers-email) ne '' )" debug="true"]<a href="mailto:[types field='speakers-email' raw='true'][/types]?subject=Inquiry from CNSV talk">[types field="name-of-speaker"][/types]</a>[/wpv-conditional]
Hey, Christian -
As I expected, using (wpcf-xxx ne '') works the same as using NOT(empty($(wpcf-xxx))). As you can see from the debug output, the comparison executes correctly; the problem is with the use of mixed single and double quotes inside the <a> tag. I've tried reversing the single for doubles, and adding quotes around "[types...][/types]" but that didn't help.
I have had trouble with this in the past with wpv-if but eventually got it to work with the correct arrangement of quotes. I can't seem to make that happen with wpv-conditional, which doesn't make sense to me because the problem is not within the wpv-conditional definition (brackets). However, to reiterate, if I simply replace [wpv-conditional...][/wpv-conditional] with [wpv-if...][/wpv-if] it works as expected.
Here is the View output:
GrokStyle: Deep Learning and the Future of Visual Search (THE POST TITLE DISPLAYS CORRECTLY HERE)
[types field="name-of-speaker"][/types] (THIS <>strong>SHOULD BE NAME OF SPEAKER WITH LINK)
####################
wpv-conditional attributes
####################
Array
(
[if] => ( $(wpcf-speakers-email) ne '' )
[debug] => true
)
####################
Debug information
####################
--------------------
Original expression: ( $(wpcf-speakers-email) ne '' ) (NE TO EMPTY STRING)
--------------------
--------------------
Converted expression: ( 'sbell@grokstyle.com' ne '' ) (THIS RESOLVES CORRECTLY AS SPEAKER'S EMAIL)
--------------------
Thanks,
Jeff Safire
Christian -
I just switched from PHP 7.03 to 5.6.20, and everything works correctly. I know little about the difference between PHP 7.0.3 and 5.6.20. Unfortunately, the whole point of this exercise of converting this content template to use wpv-conditional in place of wpv-if is to clean it up before committing to PHP 7.x because I have interspersed wpv-if and wpv-conditional in several content templates for this site.
So, it appears something different in PHP 7 affects the quotes usage/parsing inside statements containing [types][/types].
Maybe?
Jeff
----------
I don't see anything inherently wrong with this syntax. In fact, I copied it into Content Templates on two local test sites, running PHP 7.0.31 and 7.2.8, and replaced a couple of custom field slugs:
[wpv-conditional if="( $(wpcf-book-email) ne '' )"]<a href="mailto:[types field='book-email' raw='true'][/types]?subject=Inquiry from CNSV talk">[types field="author-name"][/types]</a>[/wpv-conditional]
The conditional works as expected on both sites, and the mailto link is shown with the custom field text. So there must be something else going on I'm not fully understanding. As a quick test, try adding this line to your wp-config.php file just before the line "That's all, stop editing!"
ini_set('pcre.jit', false);
Save the file and upload it to your server, then test again. If the problem is resolved, then you're bumping into an issue with regex on specific versions of PHP. Disabling the PCRE JIT regex engine will fix that problem with minimal side effects. If the problem is not resolved, then we need to do some additional investigation. Temporarily switch to a default theme like Twenty Seventeen, deactivate all plugins except Types and Views, and test again. If the problem is resolved, reactivate your theme and other plugins one by one until the problem returns.
My issue is resolved now. Thank you!