Displaying and hiding custom fields conditionally

Community Forums Forums Design Tips and Tricks Displaying and hiding custom fields conditionally

This topic is: resolved

This topic contains 3 replies, has 2 voices, and was last updated by  lucaslem 1 year, 2 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #63564

    lucaslem
    Participant
    Post count: 88

    Hello,

    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 the workshop_date, workshop_location, or registration_link fields blank, the div will not render.

    #63584

    lucaslem
    Participant
    Post count: 88

    Used 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!

    • This reply was modified 1 year, 2 months ago by  lucaslem.
    • This reply was modified 1 year, 2 months ago by  lucaslem.
    #63859

    kfukawa
    Participant
    Post count: 1

    The 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…

    #64130

    lucaslem
    Participant
    Post count: 88

    Thanks 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.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.