0

Is the following code an accepted method—-or is there a better one—-to display a list of author contact links? (Extra ones have been added to my user profile page).

<ul class="author-contact">
<?php
// Get user profile fields 
$email      = get_the_author_meta( 'email' ); 
$telephone  = get_the_author_meta( 'telephone' ); 
$twitter    = get_the_author_meta( 'twitter' ); 
$instagram  = get_the_author_meta( 'instagram' ); 
$pinterest  = get_the_author_meta( 'pinterest' ); 
$linkedin   = get_the_author_meta( 'linkedin' ); 
?>

<?php if( ! empty( $email ) ) : ?>
    <li><?php echo $email; ?></li>
<?php endif; ?>
<?php if( ! empty( $telephone ) ) : ?>
    <li><?php echo $telephone; ?></li>
<?php endif; ?>
<?php if( ! empty( $twitter ) ) : ?>
    <li><?php echo $twitter; ?></li>
<?php endif; ?>
<?php if( ! empty( $instagram ) ) : ?>
    <li><?php echo $instagram; ?></li>
<?php endif; ?>
<?php if( ! empty( $pinterest ) ) : ?>
    <li><?php echo $pinterest; ?></li>
<?php endif; ?>
<?php if( ! empty( $linkedin ) ) : ?>
    <li><?php echo $linkedin; ?></li>
<?php endif; ?>
</ul>

I am trying to reduce any duplicate entry here, and make sure the items are removed from code if not filled out in the user profile. Thanks!

1

I would actually load them into an array:

$author_contact = array(
   'email'      => get_the_author_meta( 'email' ), 
   'telephone'  => get_the_author_meta( 'telephone' ),
   'twitter'    => get_the_author_meta( 'twitter' ),
);

Once you have that created, you can iterate through it using a foreach() loop:

<ul class="author-contact">
<?php
foreach( $author_contact as $contact => $address ) {
   if( isset( $contact ) ) {
      echo '<li class="contact">' . $address . '</li>';
   }
}
?>
</ul>

What this code will do is go through all of those get_author_meta() values and store them in a single array. Then it will loop through them as the pair $contact & $address. So say it was email and the author's address was author@site.com. $contact would be email and $adress would be author@site.com.

Then it will test to see if there is a value given. So if this particular author does not have their telephone number or a Twitter address entered in, then the key would have null value. isset() will check to see if there is a value and if so, create the list item. If the value is null then it will simply skip that and move to the next key in the array.

  • Great Cedon! Needed a few code edits to make it work (won't let me amend your post). Missing closed bracket after "isset... $contact", and missing semicolon after closed list tag. Thanks! – the-first-man Feb 26 '18 at 22:41
  • One last thing, I've just realised that this method leaves empty list items if the field is empty... Is there a way to remove them? eg. "Else echo '';" (didn't work). – the-first-man Feb 26 '18 at 22:54
  • I added the missing ). You could also try setting the if() condition to if( $address != '' ) – Cedon Feb 26 '18 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.