How to use a conditional statment to not show a custom field

Hello, I created a custom single-listing.php page and was wondering if someone could help me to turn a custom field off if the user dose not input any information.

I created 4 custom fields for users to add social networks. I used an icon to represent each network. However if someone only fills out 3 of the fields its still displaying all 4 icons.

Can someone help me with an "if" or another kind of "conditional statement" to accomplish this?

My code:

<div class="listing-social"><strong>Social Profiles:</strong><br>
              <a href="<?php echo do_shortcode('[ct id="ct_Google__text_a1ff" property="title | description | value"]'); ?>" title="Find me on Google +" target="_blank">
              <div class="social-gp" alt="Google +"></div>
              </a> <a href="<?php echo do_shortcode('[ct id="ct_Facebook_text_2a8a" property="title | description | value"]'); ?>" title="Find me on Facebook" target="_blank">
              <div class="social-fb" alt="Facebook"></div>
              </a> <a href="<?php echo do_shortcode('[ct id="ct_Twitter_text_598b" property="title | description | value"]'); ?>" title="Find me on Twitter" target="_blank">
              <div class="social-tw" alt="Twitter"></div>
              </a> <a href="<?php echo do_shortcode('[ct id="ct_LinkedIn_text_d7e3" property="title | description | value"]'); ?>" title="Find me on Linkedin" target="_blank">
              <div class="social-li" alt="Linkedin"></div>
              </a> </div>
          </div>

Thanks a ton!
William

  • Vinod Dalvi

    Hi William,

    I hope you are well today and thank you for your question.

    You can achieve this by using the following code replacing your shared code. Change the page/post id in the following code with the appropriate page/post id where you are saving the social links.

    <?php
    	$page_id = 12; // or use get_the_ID();
    ?>
    <div class="listing-social"><strong>Social Profiles:</strong><br>
    <?php if( get_post_meta( $page_id, "ct_Google__text_a1ff", true) != '' ){ ?>
      <a href="<?php echo do_shortcode('[ct id="ct_Google__text_a1ff" property="value"]'); ?>" title="Find me on Google +" target="_blank">
      <div class="social-gp" alt="Google +"></div>
        </a>
    <?php } ?>
    <?php if( get_post_meta( $page_id, "ct_Facebook_text_2a8a", true) != '' ){ ?>
     <a href="<?php echo do_shortcode('[ct id="ct_Facebook_text_2a8a" property="value"]'); ?>" title="Find me on Facebook" target="_blank">
      <div class="social-fb" alt="Facebook"></div>
        </a>
    <?php } ?>
    <?php if( get_post_meta( $page_id, "ct_Twitter_text_598b", true) != '' ){ ?>
      <a href="<?php echo do_shortcode('[ct id="ct_Twitter_text_598b" property="value"]'); ?>" title="Find me on Twitter" target="_blank">
      <div class="social-tw" alt="Twitter"></div>
        </a>
    <?php } ?>
    <?php if( get_post_meta( $page_id, "ct_LinkedIn_text_d7e3", true) != '' ){ ?>
      <a href="<?php echo do_shortcode('[ct id="ct_LinkedIn_text_d7e3" property="value"]'); ?>" title="Find me on Linkedin" target="_blank">
      <div class="social-li" alt="Linkedin"></div>
      </a>
    <?php } ?>
    </div>
    </div>

    Best Regards,
    Vinod Dalvi

  • William

    Hi @Vinod Dalvi

    I dont think that worked. Its not showing any of the social icons on the page now even if they are filled in. As mentioned above this is a custom single-listing.php template. Its being used any time a user creates a listing to generate their custom page. I attached the full code below for you to look at. The Social Section starts on line 68.

    Here is a page with all the social fields populated with content: http://myinsurancepage.com/listing/debbie-elmer/ (icons should be showing up on this page)
    This is a page without all the social fields populated: http://myinsurancepage.com/listing/jubilee-schneider/ (icons should not be showing up on this page)

    Page Template Code:

    <?php get_header(); ?>
    <?php genesis_before_content_sidebar_wrap(); ?>
    <!-- Start Slider Header -->
    <div class="header-message">
      <div class="hm-one">Hello. My name is
        <?php the_title(); ?>
        .</div>
      <div class="hm-two">I specialize in <?php echo do_shortcode('[ct id="ct_Specialize_text_8abd" property="title | description | value"]'); ?>.</div>
    </div>
    <div class="header-slider"><?php echo get_new_royalslider(2); ?></div>
    <!-- End Slider Header -->
    <div id="content-sidebar-wrap">
      <?php genesis_before_content(); ?>
      <div id="content" class="hfeed">
        <?php if ( have_posts() ) : ?>
        <?php  while ( have_posts() ) : the_post(); ?>
        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
          <div class="entry-post">
            <!-- Start Content Columns -->
            <div id="leftcolumn"> <!-- Start Left Colum -->
              <div class="insurance-agent-rating">
                <div class="rating-stars">
                  <?php do_action('sr_avg_rating'); ?>
                  <!-- First Set of Stars For Average Rating -->
                  <?php if ( !is_user_logged_in() ) : ?>
                  <?php echo '<p class="must-log-in">' .  sprintf( __( 'You must be <a href="%s">logged in</a> to rate agent.', DR_TEXT_DOMAIN), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post->ID ) ) ) ) . '</p>'; ?> <!-- You must be *logged in* to rate agent. -->
                  <?php else: ?>
                  <?php do_action('sr_rate_this'); ?>
                  <!-- Rating stars *If logged in -->
                  <?php endif; ?>
                </div>
                <p> <span class="comments">
                  <?php comments_popup_link(
    						__( 'No Reviews »', DR_TEXT_DOMAIN ),
    						__( '1 Review »', DR_TEXT_DOMAIN ),
    						__( '% Reviews »', DR_TEXT_DOMAIN ),
    						'',
    						__( 'Reviews Off;', DR_TEXT_DOMAIN )
    					); ?>
                  </span><br />
                </p>
              </div>
              <div class="listing-img">
                <?php if( has_post_thumbnail() ): ?>
                <?php the_post_thumbnail( array( 500, 500 ), array( 'class' => 'alignleft' ) ); ?>
                <?php endif; ?>
              </div>
              <div id="listingHeader">
                <div id="listingTitle">
                  <h1>
                    <?php the_title(); ?>
                  </h1>
                  <div class="lic-number">Lic # <?php echo do_shortcode('[ct id="ct_Lic__text_2f1e" property="title | description | value"]'); ?></div>
                  <div class="year-lic">Year Licenced: <?php echo do_shortcode('[ct id="ct_Year_Licen_text_b69a" property="title | description | value"]'); ?></div>
                  <!--?php do_action('sr_avg_rating'); ?-->
                </div>
              </div>
              <!--div class="listing-excerpt">
              	<!--?php the_excerpt(); ?>
              </div-->
              <div class="listing-contact-container">
                <div class="listing-web"><strong>Web:</strong> <a href="<?php echo do_shortcode('[ct id="_ct_text_54beb6667a612" property="title | description | value"]'); ?>" title="Website Link" target="_blank"><?php echo do_shortcode('[ct id="_ct_text_54beb6667a612" property="title | description | value"]'); ?></a></div>
                <div class="listing-phone"><strong>Phone:</strong> <a href="tel:<?php echo do_shortcode('[ct id="_ct_text_54beb772e30d9" property="title | description | value"]'); ?> " title="Phone Number"><?php echo do_shortcode('[ct id="_ct_text_54beb772e30d9" property="title | description | value"]'); ?></a></div>
                <div class="listing-email"><strong>Email:</strong> <a href="mailto:<?php echo do_shortcode('[ct id="_ct_text_54beb7973039c" property="title | description | value"]'); ?>" title="Website Link" target="_blank"><?php echo do_shortcode('[ct id="_ct_text_54beb7973039c" property="title | description | value"]'); ?></a></div>
                <div class="listing-address"><strong>Address:</strong><br>
                  <?php echo do_shortcode('[ct id="_ct_textarea_54beb7f635c6f" property="title | description | value"]'); ?><br>
                  <?php echo do_shortcode('[ct id="_ct_text_54befdd39baf3" property="title | description | value"]'); ?>, <?php echo do_shortcode('[ct id="_ct_text_54befe359a50a" property="title | description | value"]'); ?> <?php echo do_shortcode('[ct id="_ct_text_54befe727214a" property="title | description | value"]'); ?></div>
                <!--Start Social Section-->
                <?php get_the_ID(); //$page_id = 12; // or use get_the_ID(); ?>
                <div class="listing-social"><strong>Social Profiles:</strong><br>
                  <?php if( get_post_meta( $page_id, "ct_Google__text_a1ff", true) != '' ){ ?>
                  <a href="<?php echo do_shortcode('[ct id="ct_Google__text_a1ff" property="value"]'); ?>" title="Find me on Google +" target="_blank">
                  <div class="social-gp" alt="Google +"></div>
                  </a>
                  <?php } ?>
                  <?php if( get_post_meta( $page_id, "ct_Facebook_text_2a8a", true) != '' ){ ?>
                  <a href="<?php echo do_shortcode('[ct id="ct_Facebook_text_2a8a" property="value"]'); ?>" title="Find me on Facebook" target="_blank">
                  <div class="social-fb" alt="Facebook"></div>
                  </a>
                  <?php } ?>
                  <?php if( get_post_meta( $page_id, "ct_Twitter_text_598b", true) != '' ){ ?>
                  <a href="<?php echo do_shortcode('[ct id="ct_Twitter_text_598b" property="value"]'); ?>" title="Find me on Twitter" target="_blank">
                  <div class="social-tw" alt="Twitter"></div>
                  </a>
                  <?php } ?>
                  <?php if( get_post_meta( $page_id, "ct_LinkedIn_text_d7e3", true) != '' ){ ?>
                  <a href="<?php echo do_shortcode('[ct id="ct_LinkedIn_text_d7e3" property="value"]'); ?>" title="Find me on Linkedin" target="_blank">
                  <div class="social-li" alt="Linkedin"></div>
                  </a>
                  <?php } ?>
                </div>
              </div>
              <!--End Social Section-->
            </div>
            <!-- End Left Colum -->
            <div id="rightcolumn"> <!-- Start Right Column -->
              <div class="topright">
                <div class="toprightone">
                  <h2>About Me:</h2>
                  <?php the_content(); ?>
                </div>
                <div class="toprighttwo">
                  <h2>About My Company:</h2>
                  <?php echo do_shortcode('[ct id="ct_About_My_C_textarea_9046" property="title | description | value"]'); ?>
                  <div class="company-logo"><?php echo do_shortcode('[ct id="ct_Upload_Com_upload_50e0" property="title | description | value"]'); ?></div>
                </div>
              </div>
              <div class="bottomright">
                <div class="listing-services">
                  <h3>Insurance Services Provided:</h3>
                  <div class="provided-services"><?php echo do_shortcode('[tax id="insurance_services" separator=", " ]'); ?></div>
                </div>
                <div class="insurance-agent-designations">
                  <h3>Designations:</h3>
                  <div class="designations"> <?php echo do_shortcode('[ct id="ct_Designatio_textarea_4467" property="title | description | value"]'); ?> </div>
                </div>
                <div class="insurance-agent-expertise">
                  <h3>Expertise:</h3>
                  <div class="expertise"> <?php echo do_shortcode('[ct id="ct_expertise_textarea_c898" property="title | description | value"]'); ?> </div>
                </div>
              </div>
            </div>
          </div>
          <!-- End Right Colum --> 
    
        </div>
      </div>
      <div id="commentbox">
        <?php comments_template('', true); ?>
      </div>
      <?php endwhile; ?>
      <div id="post-navigator">
        <div class="alignleft">
          <?php next_posts_link( __( '« Previous Entries', DR_TEXT_DOMAIN ) ) ?>
        </div>
        <div class="alignright">
          <?php previous_posts_link( __( 'Next Entries »', DR_TEXT_DOMAIN ) ) ?>
        </div>
      </div>
      <?php else : ?>
      <h3>
        <?php _e("Sorry, we can't find the post you're looking for at this URL. Please try selecting a menu item from above or to the side of this message to get where you'd like to go.", DR_TEXT_DOMAIN); ?>
      </h3>
      <?php endif; ?>
    </div>
    <!-- end #blog-single -->
    </div>
    </div>
    <!-- end #content -->
    <?php get_footer(); ?>

    Also is there any way to make the header "Social Profiles:" not show up if non of the fields are populated?

    Thanks,
    William

  • Ash

    Hello @William

    I hope you are well today.

    In stead of this:

    <?php get_the_ID(); //$page_id = 12; // or use get_the_ID(); ?>

    try the following:

    <?php
    global $post;
    $page_id = $post->ID;
    ?>

    or this:

    <?php
    $page_id = get_the_ID();
    ?>

    The first one is preferred.

    Also is there any way to make the header "Social Profiles:" not show up if non of the fields are populated?

    Try the following code as template:

    <?php get_header(); ?>
    <?php genesis_before_content_sidebar_wrap(); ?>
    <!-- Start Slider Header -->
    <div class="header-message">
      <div class="hm-one">Hello. My name is
        <?php the_title(); ?>
        .</div>
      <div class="hm-two">I specialize in <?php echo do_shortcode('[ct id="ct_Specialize_text_8abd" property="title | description | value"]'); ?>.</div>
    </div>
    <div class="header-slider"><?php echo get_new_royalslider(2); ?></div>
    <!-- End Slider Header -->
    <div id="content-sidebar-wrap">
      <?php genesis_before_content(); ?>
      <div id="content" class="hfeed">
        <?php if ( have_posts() ) : ?>
        <?php  while ( have_posts() ) : the_post(); ?>
        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
          <div class="entry-post">
            <!-- Start Content Columns -->
            <div id="leftcolumn"> <!-- Start Left Colum -->
              <div class="insurance-agent-rating">
                <div class="rating-stars">
                  <?php do_action('sr_avg_rating'); ?>
                  <!-- First Set of Stars For Average Rating -->
                  <?php if ( !is_user_logged_in() ) : ?>
                  <?php echo '<p class="must-log-in">' .  sprintf( __( 'You must be <a href="%s">logged in</a> to rate agent.', DR_TEXT_DOMAIN), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post->ID ) ) ) ) . '</p>'; ?> <!-- You must be *logged in* to rate agent. -->
                  <?php else: ?>
                  <?php do_action('sr_rate_this'); ?>
                  <!-- Rating stars *If logged in -->
                  <?php endif; ?>
                </div>
                <p> <span class="comments">
                  <?php comments_popup_link(
    						__( 'No Reviews »', DR_TEXT_DOMAIN ),
    						__( '1 Review »', DR_TEXT_DOMAIN ),
    						__( '% Reviews »', DR_TEXT_DOMAIN ),
    						'',
    						__( 'Reviews Off;', DR_TEXT_DOMAIN )
    					); ?>
                  </span><br />
                </p>
              </div>
              <div class="listing-img">
                <?php if( has_post_thumbnail() ): ?>
                <?php the_post_thumbnail( array( 500, 500 ), array( 'class' => 'alignleft' ) ); ?>
                <?php endif; ?>
              </div>
              <div id="listingHeader">
                <div id="listingTitle">
                  <h1>
                    <?php the_title(); ?>
                  </h1>
                  <div class="lic-number">Lic # <?php echo do_shortcode('[ct id="ct_Lic__text_2f1e" property="title | description | value"]'); ?></div>
                  <div class="year-lic">Year Licenced: <?php echo do_shortcode('[ct id="ct_Year_Licen_text_b69a" property="title | description | value"]'); ?></div>
                  <!--?php do_action('sr_avg_rating'); ?-->
                </div>
              </div>
              <!--div class="listing-excerpt">
              	<!--?php the_excerpt(); ?>
              </div-->
              <div class="listing-contact-container">
                <div class="listing-web"><strong>Web:</strong> <a href="<?php echo do_shortcode('[ct id="_ct_text_54beb6667a612" property="title | description | value"]'); ?>" title="Website Link" target="_blank"><?php echo do_shortcode('[ct id="_ct_text_54beb6667a612" property="title | description | value"]'); ?></a></div>
                <div class="listing-phone"><strong>Phone:</strong> <a href="tel:<?php echo do_shortcode('[ct id="_ct_text_54beb772e30d9" property="title | description | value"]'); ?> " title="Phone Number"><?php echo do_shortcode('[ct id="_ct_text_54beb772e30d9" property="title | description | value"]'); ?></a></div>
                <div class="listing-email"><strong>Email:</strong> <a href="mailto:<?php echo do_shortcode('[ct id="_ct_text_54beb7973039c" property="title | description | value"]'); ?>" title="Website Link" target="_blank"><?php echo do_shortcode('[ct id="_ct_text_54beb7973039c" property="title | description | value"]'); ?></a></div>
                <div class="listing-address"><strong>Address:</strong><br>
                  <?php echo do_shortcode('[ct id="_ct_textarea_54beb7f635c6f" property="title | description | value"]'); ?><br>
                  <?php echo do_shortcode('[ct id="_ct_text_54befdd39baf3" property="title | description | value"]'); ?>, <?php echo do_shortcode('[ct id="_ct_text_54befe359a50a" property="title | description | value"]'); ?> <?php echo do_shortcode('[ct id="_ct_text_54befe727214a" property="title | description | value"]'); ?></div>
                <!--Start Social Section-->
                <?php
    	    global $post;
    	    $page_id = $post->ID;
    	    ?>
    	    <?php if( get_post_meta( $page_id, "ct_Google__text_a1ff", true) != '' || get_post_meta( $page_id, "ct_Facebook_text_2a8a", true) != '' || get_post_meta( $page_id, "ct_Twitter_text_598b", true) != '' || get_post_meta( $page_id, "ct_LinkedIn_text_d7e3", true) != '' ) { ?>
                <div class="listing-social"><strong>Social Profiles:</strong><br>
                  <?php if( get_post_meta( $page_id, "ct_Google__text_a1ff", true) != '' ){ ?>
                  <a href="<?php echo do_shortcode('[ct id="ct_Google__text_a1ff" property="value"]'); ?>" title="Find me on Google +" target="_blank">
                  <div class="social-gp" alt="Google +"></div>
                  </a>
                  <?php } ?>
                  <?php if( get_post_meta( $page_id, "ct_Facebook_text_2a8a", true) != '' ){ ?>
                  <a href="<?php echo do_shortcode('[ct id="ct_Facebook_text_2a8a" property="value"]'); ?>" title="Find me on Facebook" target="_blank">
                  <div class="social-fb" alt="Facebook"></div>
                  </a>
                  <?php } ?>
                  <?php if( get_post_meta( $page_id, "ct_Twitter_text_598b", true) != '' ){ ?>
                  <a href="<?php echo do_shortcode('[ct id="ct_Twitter_text_598b" property="value"]'); ?>" title="Find me on Twitter" target="_blank">
                  <div class="social-tw" alt="Twitter"></div>
                  </a>
                  <?php } ?>
                  <?php if( get_post_meta( $page_id, "ct_LinkedIn_text_d7e3", true) != '' ){ ?>
                  <a href="<?php echo do_shortcode('[ct id="ct_LinkedIn_text_d7e3" property="value"]'); ?>" title="Find me on Linkedin" target="_blank">
                  <div class="social-li" alt="Linkedin"></div>
                  </a>
                  <?php } ?>
                </div>
    	    <?php } ?>
              </div>
              <!--End Social Section-->
            </div>
            <!-- End Left Colum -->
            <div id="rightcolumn"> <!-- Start Right Column -->
              <div class="topright">
                <div class="toprightone">
                  <h2>About Me:</h2>
                  <?php the_content(); ?>
                </div>
                <div class="toprighttwo">
                  <h2>About My Company:</h2>
                  <?php echo do_shortcode('[ct id="ct_About_My_C_textarea_9046" property="title | description | value"]'); ?>
                  <div class="company-logo"><?php echo do_shortcode('[ct id="ct_Upload_Com_upload_50e0" property="title | description | value"]'); ?></div>
                </div>
              </div>
              <div class="bottomright">
                <div class="listing-services">
                  <h3>Insurance Services Provided:</h3>
                  <div class="provided-services"><?php echo do_shortcode('[tax id="insurance_services" separator=", " ]'); ?></div>
                </div>
                <div class="insurance-agent-designations">
                  <h3>Designations:</h3>
                  <div class="designations"> <?php echo do_shortcode('[ct id="ct_Designatio_textarea_4467" property="title | description | value"]'); ?> </div>
                </div>
                <div class="insurance-agent-expertise">
                  <h3>Expertise:</h3>
                  <div class="expertise"> <?php echo do_shortcode('[ct id="ct_expertise_textarea_c898" property="title | description | value"]'); ?> </div>
                </div>
              </div>
            </div>
          </div>
          <!-- End Right Colum --> 
    
        </div>
      </div>
      <div id="commentbox">
        <?php comments_template('', true); ?>
      </div>
      <?php endwhile; ?>
      <div id="post-navigator">
        <div class="alignleft">
          <?php next_posts_link( __( '« Previous Entries', DR_TEXT_DOMAIN ) ) ?>
        </div>
        <div class="alignright">
          <?php previous_posts_link( __( 'Next Entries »', DR_TEXT_DOMAIN ) ) ?>
        </div>
      </div>
      <?php else : ?>
      <h3>
        <?php _e("Sorry, we can't find the post you're looking for at this URL. Please try selecting a menu item from above or to the side of this message to get where you'd like to go.", DR_TEXT_DOMAIN); ?>
      </h3>
      <?php endif; ?>
    </div>
    <!-- end #blog-single -->
    </div>
    </div>
    <!-- end #content -->
    <?php get_footer(); ?>

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • William

    Hi @Ashok,

    This worked perfectly. I was also able to incorporate your conditional statements to other parts of the template as well. If you dont mind me asking could you explain why the code worked?

    Im still learning php and trying to understand what everything means and how it works....

    Why did you need to call out the page_id/post->ID?

    <?php
    global $post;
    $page_id = $post->ID;
    ?>

    Once called out how dose the code below work with each other?

    <?php if( get_post_meta( $page_id, "ct_Google__text_a1ff", true) != ''  ) { ?>
                <div class="listing-social"><strong>Social Profiles:</strong><br>
                  <?php if( get_post_meta( $page_id, "ct_Google__text_a1ff", true) != '' ){ ?>
                  <a href="<?php echo do_shortcode('[ct id="ct_Google__text_a1ff" property="value"]'); ?>" title="Find me on Google +" target="_blank">
                  <div class="social-gp" alt="Google +"></div>
                  </a>
                  <?php } ?>

    Thanks again for your help and as always you guys rock! :slight_smile:

    William

  • Vinod Dalvi

    Hi William,

    Glad it's working fine for you now :slight_smile:

    The get_the_ID function only works in the WordPress loop and i assumed you are using the shared code in the WordPress loop.

    Outside the WordPress loop instead of using get_the_ID function, you should use the code like following.

    <?php
    global $post;
    $page_id = $post->ID;
    ?>

    Or

    <?php
    global $wp_query;
    $page_id = $wp_query->post->ID;
    ?>

    Cheers,
    Vinod Dalvi

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.