Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to show the profile fields (like AIM, Jibber) outside the loop. I managed to do that with

<?php
global $post;
$author_id=$post->post_author;
?>

<?php
$field='aim';
the_author_meta( $field, $author_id );
?>

But now, I can't put an image beside it if such a field exists. This works inside the loop, but not outside:

<?php if (get_the_author_meta('aim')) {
echo "

<div class=schrijver-socialmedia>
  <img src=http://blablabla.com/images/aim_icon.png /> 

"; } ?>

How do I get this image outside the loop and only if the field exists?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

get_the_author_meta accepts two parameters

  • $field - Field name for the data item to be returned.
  • $userID - If a user ID is passed to the function, it will return data for the specified user ID.

So just pass the user ID or author ID eg:

 if (get_the_author_meta('aim',$author_id)) { ...
share|improve this answer
    
Worked. Thanks! –  user1627363 May 28 '13 at 10:42

get_the_author_meta is a wrapper around get_user_meta, which is what you should use instead.

$value = get_user_meta( $user_id, $key, $single );

$single is a true or false value, if you want a single meta field, pass true. If you have multiple fields of the same name, pass false, and it will return an array instead.

Since you only have a single field, named aim, pass in true

e.g.

$aim = get_user_meta( $author_id, 'aim', true );

Here $author_id is the user ID of the author/user you're querying.

To check if the user has an aim, check the $aim value. get_user_meta will return false if no meta of that key was found.

if ( $aim != false ) {
    // do stuff
}

But what if there is an aim field and that field is empty? Then we can make use of empty() to check both cases, resulting in:

$aim = get_user_meta( $author_id, 'aim', true );
if ( !empty( $aim ) ) {
    // do stuff
}

Then of course you could always pass in the author ID as the second parameter to get_the_author_meta

share|improve this answer
    
Thanks mate. Also a very clear answer. –  user1627363 May 28 '13 at 10:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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