Views is great for helping people who don’t want to write PHP code set up custom templates, but sometimes the documentation isn’t as clear as it could be. Recently I had a situation where a client wanted to be able to upload a pdf of a book review with a link to open the review, but if there was no pdf for that particular review, we didn’t want the “read review” link to show. In order to do that, I needed to use a conditional statement. Here’s what I did:
Originally, I had this statement in my View to show the link for the pdf:
Read review
You can see that I have named my WP-Types custom field “paper”. I have several reviews, so when I put this code in the Loop Output section of my View, it gets each pdf review and links it. Put this means it displays each time there is a review, whether or not there is a pdf to go along with it. So, to keep it from displaying if no pdf had been uploaded, I had to add this conditional shortcode:
[wpv-if upload="wpcf-paper" evaluate="!empty($upload)"] Read review [/wpv-if]
You can see my original link is still there, but now I have wrapped it with the conditional shortcode. For Views, that shortcode is:
[wpv-if][/wpv-if]
within that shortcode, I needed to create a new variable. You can see I have “upload”. This word I just made up. It could be anything and I assigned it to the custom field which is “wpcf-paper”. (Whenever you use a WP-Types custom field, you need to put that “wpcf” in front of it.) Then I had to evaluate if it was not empty. To do that I used the exclamation point, which means “not” in PHP code and then the term “empty”. And then I called the new variable that I created “$upload”. Once this is in place, it makes sure that the custom field “paper” is not empty, and if it’s not, then it displays my code.
You can see an example of it here at the bottom of the page:
Leave a Reply