• Resolved brycges

    (@brycges)


    Hi guys,

    I need a way to not display something if the meta field is empty for the user.

    Here’s what I’m doing.

    I’ve created a few custom fields, and am displaying them in their users pages. I need away to not display icons for social networks if their not filled. here my code.

    <?php
      $key = 'facebook';
      $single = true;
      $user_last = get_user_meta( $user_id, $key, $single );
     echo '<a href="' . $facebook . '"><img src="/img/facebook.png" width="43" height="42" /></a>   ';
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • C W (VYSO)

    (@cyril-washbrook)

    <?php
       $key = 'facebook';
       $single = true;
       $user_last = get_user_meta( $user_id, $key, $single );
       if (!empty($user_last)) :
          echo '<a href="' . $facebook . '"><img src="/img/facebook.png" width="43" height="42" /></a>   ';
       endif; ?>

    The only other (cosmetic) modification I would consider is not echoing out that HTML fragment with PHP, but instead outputting it like this:

    <?php
       $key = 'facebook';
       $single = true;
       $user_last = get_user_meta( $user_id, $key, $single );
       if (!empty($user_last)) : ?>
          <a href="<?php echo $facebook; ?>"><img src="/img/facebook.png" width="43" height="42" /></a>
       <?php endif; ?>

    In this case it may not look like much of an improvement, but in general, creating HTML parts with PHP makes your code less readable.

    Thread Starter brycges

    (@brycges)

    Thanks, your a life saver!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘how to not display meta if empty’ is closed to new replies.