WordPress.org

Ready to get started?Download WordPress

Forums

Hiding Content (5 posts)

  1. craig.eves
    Member
    Posted 2 years ago #

    Hi

    I have set up a custom admin screen in WordPress and i'm pulling in the content using the following code:

    <!-- OPENING HOURS -->
    <h2>Opening Hours</h2>
    <ul>
      <?php if($options['monday'] !='') { ?>
      <li>Monday: <?php echo $options['monday']; ?></li>
      <?php } ?>
      <?php if($options['tuesday'] !='') { ?>
      <li>Tuesday: <?php echo $options['tuesday']; ?></li>
      <?php } ?>
     <?php if($options['wednesday'] !='') { ?>
      <li>Wednesday: <?php echo $options['wednesday']; ?></li>
      <?php } ?>
    <?php if($options['thursday'] !='') { ?>
      <li>Thursday: <?php echo $options['thursday']; ?></li>
      <?php } ?>
    <?php if($options['friday'] !='') { ?>
      <li>Friday: <?php echo $options['friday']; ?></li>
      <?php } ?>
    <?php if($options['saturday'] !='') { ?>
      <li>Saturday: <?php echo $options['saturday']; ?></li>
      <?php } ?>
    <?php if($options['sunday'] !='') { ?>
      <li>Sunday: <?php echo $options['sunday']; ?></li>
      <?php } ?>
    </ul>

    I wondered how I would alter this code so that if none of the fields are filled in on the admin screen then nothing shows - including the <h2>Opening Hours</h2>.

    I'm assuming it's something like this, but i'm not sure...

    <?php if($options['monday'&&'tuesday'&&'wednesday'&&'thursday'&&'friday'&&'saturday'&&'sunday'] !='') { ?>
      <p>Content Here</p>
      <?php } ?>

    Can someone shed some light?

  2. craig.eves
    Member
    Posted 2 years ago #

    Just for the record - the code works already as is - but if all the input fields are empty on the admin panel then the <h2>Opening Hours</h2> still shows, but i don't want it too...

  3. Peter_L
    Member
    Posted 2 years ago #

    <?php if(($options['monday'] !='') && ($options['tuesday'] !='') && ...fill... && ($options['sunday'] !='')){ ?>
    <!-- OPENING HOURS -->
    <h2>Opening Hours</h2>
    <ul>
    ...
    </ul>
    <?php } ?>

    There are simpler ways to do this, but this should work fine for you.

  4. craig.eves
    Member
    Posted 2 years ago #

    For some reason that didn't work...

    I have

    <?php if(($options['facebook'] !='') && ($options['twitter'] !='') && ($options['flickr'] !='') && ($options['googleplus'] !='')){ ?>
    <!-- SOCIAL NETWORKS -->
    <h2>Social Networks</h2>
    <ul>
      <?php if($options['facebook'] !='') { ?>
      <li><a href="<?php echo $options['facebook']; ?>" target="_blank">Like us on Facebook</a></li>
      <?php } ?>
      <?php if($options['twitter'] !='') { ?>
      <li><a href="<?php echo $options['twitter']; ?>" target="_blank">Follow us on Twitter</a></li>
      <?php } ?>
      <?php if($options['flickr'] !='') { ?>
      <li><a href="<?php echo $options['flickr']; ?>" target="_blank">Find us on Flickr</a></li>
      <?php } ?>
      <?php if($options['googleplus'] !='') { ?>
      <li><a href="<?php echo $options['googleplus']; ?>" target="_blank">+1 us on Google+</a></li>
      <?php } ?>
    </ul>
    <?php } ?>

    If none of the fields are filled in I don't want to show anything... currently this code shows nothing regardless of whether the fields are filled in or not

  5. Peter_L
    Member
    Posted 2 years ago #

    Rule 1 returns true if and only if all the options are not empty.
    So, if one of the options is empty, the condional will be false and nothing will happen.

    The solution is simple. You want to know if at least one of the options is NOT empty. So, on rule 1, replace the and selector && with the or selector ||.
    This will return true if one of the options is not empty.

Topic Closed

This topic has been closed to new replies.

About this Topic