Community Forums › Forums › Archived Forums › Design Tips and Tricks › Displaying and hiding custom fields conditionally
Tagged: Custom fields, Custom Post Type
- This topic has 3 replies, 2 voices, and was last updated 11 years, 4 months ago by
lucaslem.
-
AuthorPosts
-
September 20, 2013 at 1:13 pm #63564
lucaslem
MemberHello,
I have created a block of code in order to display some custom fields for a cpt:
//* Custom fields for workshop intro and registration call to action add_action( 'genesis_before_entry_content', 'ismh_workshops_acf' ); function ismh_workshops_acf() { if ( is_single() && 'workshops' == get_post_type() ) echo '<p class="workshop-intro">' . genesis_get_custom_field('workshop_intro') . '</p> <div class="registration-cta clearfix"> <div class="workshop-info"> <p class="workshop-info-data"><span class="workshop-info-callout">Date: </span>' . genesis_get_custom_field('workshop_date') . '</p> <p class="workshop-info-data"><span class="workshop-info-callout">Location: </span>' . genesis_get_custom_field('workshop_location') . '</p> </div> <a class="button" href="' . genesis_get_custom_field('registration_link') . '">Register Now!</a> </div>'; }
Not every workshop will be scheduled at all times. Is there some conditional code I could add which would prevent the entire
.registration-cta
div from displaying when the three custom fields within it are empty? In other words, when the editor leaves theworkshop_date
,workshop_location
, orregistration_link
fields blank, the div will not render.September 20, 2013 at 3:55 pm #63584lucaslem
MemberUsed the ACF repeater fields addon and pretty sure I've solved my own issue:
//* Workshop registration call to action add_action( 'genesis_before_entry_content', 'ismh_workshops_registration_acf' ); function ismh_workshops_registration_acf() { if ( is_single() && 'workshops' == get_post_type() && get_field('registration_details') ) while( has_sub_field('registration_details') ): echo '<div class="registration-cta clearfix"> <div class="workshop-info"> <p class="workshop-info-data"><span class="workshop-info-callout">Date: </span>' . get_sub_field('workshop_date') . '</p> <p class="workshop-info-data"><span class="workshop-info-callout">Location: </span>' . get_sub_field('workshop_location') . '</p> </div> <a class="button" href="' . get_sub_field('registration_link') . '">Register Now!</a> </div>'; endwhile; }
Turns out when the fields are blank nothing displays 🙂 The learning curve is steep, but I'm getting there! If anyone sees anything horrendous about this code, please do chime in. Thanks!
September 23, 2013 at 11:19 am #63859kfukawa
MemberThe only thing I'd worry about is if only one of the sub-fields (i.e. either Date or Location) was entered, but not the other - unless you don't mind having one of them blank.
You could always do something like:
if ( genesis_get_custom_field('workshop_date') )
echo '<p class="workshop-info-data"><span class="workshop-info-callout">Date: </span>' . get_sub_field('workshop_date') . '</p>';and the same thing for location...
September 25, 2013 at 1:11 pm #64130lucaslem
MemberThanks kfukawa, you're absolutely right. This case didn't occur to me as it's highly unlikely, given these are scheduled workshops, that they would have a date without a location, link etc (or any combination). However, I will refactor the code as you suggest just in case.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.