Advanced Custom Fields – bbird.me http://bbird.me WordPress quick tips Wed, 08 Aug 2018 16:39:07 +0000 en-US hourly 1 https://wordpress.org/?v=4.9.8 How to set conditional logic in Advanced Custom Fields http://bbird.me/how-to-set-conditional-logic-in-advanced-custom-fields/ http://bbird.me/how-to-set-conditional-logic-in-advanced-custom-fields/#respond Mon, 15 Feb 2016 20:34:04 +0000 http://bbird.me/?p=321 If you tried to set conditional logic using Advanced Custom Fields plugin, but haven't done it before, there's a chance that you will stumble upon the following message "No toggle fields available" wondering what that means. Fortunately, it is very easy to set this up. In order to add toggle fields, you would only need to create a new custom field of "choice" Field Type and add few Choices.

Post How to set conditional logic in Advanced Custom Fields je prvi puta viđen na bbird.me.

]]>
If you tried to set conditional logic using Advanced Custom Fields plugin, but haven’t done it before, there’s a chance that you will stumble upon the following message “No toggle fields available” wondering what that means. Fortunately, it is very easy to set this up. In order to add toggle fields, you would only need to create a new custom field of “choice” Field Type and add few Choices.

custom fields1

For this particular example, I used “Select” choice, creating select custom field type. Into Choices, let’s add few options:

  • none
  • first choice
  • second choice

The third step is to create few custom fields and attach the conditional logic on them. It isn’t important at this point  what kind of fields do we have, as it could be  anything. You could set one custom field to show the post URL, and the other one to show custom URL.

custom fields 2

But how would you display custom fields depending on the choice being made? As you probably know, the_field() will display custom field value, but we can also find some use of get_field() here. This is how we could write the PHP function:

<?php
if ( 'none' == get_field('custom_url_toggle')){
    //do nothing
} elseif ( 'first choice' == get_field('custom_url_toggle') ) {
    the_field("custom_url");

} elseif ( 'second choice' == get_field('custom_url_toggle') ) {
   the_field("regular_url");
} 
?>

This function will display nothing if “none” is selected, it will display the content of custom_url field if “first choice”is selected and  regular_url value if the “second choice”is selected.

Post How to set conditional logic in Advanced Custom Fields je prvi puta viđen na bbird.me.

]]>
http://bbird.me/how-to-set-conditional-logic-in-advanced-custom-fields/feed/ 0