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'm using simple code to show user social link in author bio. in functions.php

<?php
function add_remove_contactmethods( $contactmethods ) {
    // Add Twitter
    $contactmethods['twitter'] = 'Twitter';
    //Add Facebook
    $contactmethods['facebook'] = 'Facebook';
 // Remove Contact Methods
    unset($contactmethods['aim']);
    unset($contactmethods['yim']);
    unset($contactmethods['jabber']);

    return $contactmethods;
}
add_filter('user_contactmethods','add_remove_contactmethods',10,1);
?>

in single.php

   <a href="<?php the_author_meta('twitter'); ?>" title="Twitter" target="_blank" id="twitter"><img src="/images/twitter.png" alt="Twitter" /></a>

so how can i hide social link in author bio when the field is empty in user profile.

please help me...

share|improve this question
add comment

1 Answer

You need to check, if the field is empty or not before printing link using the get_the_author_meta function.

<?php if(!empty(get_the_author_meta('twitter'))) { ?>
   <a href="<?php the_author_meta('twitter'); ?>" title="Twitter" target="_blank" id="twitter"><img src="/images/twitter.png" alt="Twitter" /></a>
<?php } ?>

or, try

<?php if(!empty(get_user_meta(get_the_author_meta('ID'),'twitter'))) { ?>
   <a href="<?php the_author_meta('twitter'); ?>" title="Twitter" target="_blank" id="twitter"><img src="/images/twitter.png" alt="Twitter" /></a>
<?php } ?>

but, for some reasons, followed code fixed it

<?php if(strlen(get_the_author_meta('twitter')) >5) { ?>
   <a href="<?php the_author_meta('twitter'); ?>" title="Twitter" target="_blank" id="twitter"><img src="/images/twitter.png" alt="Twitter" /></a>
<?php } ?>
share|improve this answer
    
Thank you for your answer but they still not working, the field is empty. :( –  Maan Dec 13 '12 at 14:06
    
1) Please make sure it does not even have a "space". 2) check output of "var_dump(get_the_author_meta('twitter'))" to see what it has that makes the if condition true. –  M-R Dec 13 '12 at 14:10
    
okay here is the update: the field is empty, when i use that code "var_dump(get_the_author_meta('twitter'))" i have got the error string(0) "" –  Maan Dec 13 '12 at 14:20
    
Can someone please give me the right code... –  Maan Dec 13 '12 at 19:29
1  
Thanks M-R That code (strlen(get_the_author_meta('twitter')) >5) are now work perfectly . –  Maan Dec 13 '12 at 19:55
show 2 more comments

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.