• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Displaying and hiding custom fields conditionally

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

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

This topic is: resolved

Tagged: Custom fields, Custom Post Type

  • This topic has 3 replies, 2 voices, and was last updated 9 years, 8 months ago by lucaslem.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • September 20, 2013 at 1:13 pm #63564
    lucaslem
    Member

    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.

    September 20, 2013 at 3:55 pm #63584
    lucaslem
    Member

    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!

    September 23, 2013 at 11:19 am #63859
    kfukawa
    Member

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

    September 25, 2013 at 1:11 pm #64130
    lucaslem
    Member

    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.

  • Author
    Posts
Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Design Tips and Tricks’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2023 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble