Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Using a conditional to display field content

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #164847
    rouviere
    Participant

    I want to be able to display a div and its content only if the field has something in it. In this case, it is a photo.

    I would like to do something like this:

    if the lead_photo field is not empty, display the following:

    <div class="article-photo col-sm-9">
      <img class="img-responsive pull-right" src="<?php the_field('lead_photo'); ?>" alt="" />
    </div>
    

    What is the correct way to do this?

    Thanks.

    #164851
    Alen
    Participant
      <?php
    
      // Cache the resource
      $photo = the_field('lead_photo');
      // Perform if statement
      // 'Close' PHP, insert our HTML
      if ($photo) { ?>
        <div class="article-photo col-sm-9">
          <img class="img-responsive pull-right" src="<?php echo $photo; ?>" alt="" />
        </div>
      // 'Open' PHP, and close the if statement
      <?php } ?>
    

    Also check out WordPress specific conditional statements

    #164852
    rouviere
    Participant

    Thank you both for your feedback. I am always grateful when people take time to respond to my inquiries.

    Doc, your solution was spot on.

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Back End’ is closed to new topics and replies.