Set value of a custom field based on conditions of another field
I am trying to display the value of a field called “phone_number_to_show” in front-end content. I plan on using a shortcode or Kadence block custom field insertion to display the value of this field.
I want “phone_number_to_show” field to output the value of one or the other of two fields:
A text field “555_number” is set on the post editor as value = “555-555-5555”
A text field “444_number” is set on the post editor as value = “444-444-4444”
I want to give an editor the choice of which field is used to populate the value of “phone_number_to_show” by setting the boolean value of an ACF checkbox field called “show_555_number”.
If “show_555_number” is checked “true” by the editor, then when the field “phone_number” is used in content, I am thinking that we’d need code to update the “phone_number_to_show” field on page load to the value of “555_number”, and the front-end content would show “555-555-5555”. Else, it would display the value of “444_number”, and the front-end content would show “444-444-4444”.
I am trying to figure out the proper code block to put into functions.php to have this happen and haven’t had much luck.
If you have any ideas on how to set the value of “phone_number_to_show” based on the selection of another field, I would really appreciate it.
Bonus – if you have an idea on how to set the value of “phone_number_to_show” to pull from the key of a single-select field, instead of in reference to the boolean, that would be even better so that we could select from a list of any size on which “phone_number_to_show”.
Leave an answer