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…
You need to check, if the field is empty or not before printing link using the get_the_author_meta function.
or, try
but, for some reasons, followed code fixed it
Here is a sophisticated way of creating the social links and then retrieving them back where you need using a simple function. You can use this function to create a shortcode too. (I copied the code from one of my project, so you might want to change some of the class prefixes if you like).
Now all you need to do is to do is to put this function where you want (content-single.php maybe)
Of course you can extend the use of conditional sentences to hide the whole when there is no social links provided for any of the fields.
Here is how you can create a shortcode from the code above:
The first script: getSocialLinksByAuthor returns an array of social links based on the array: $socialMetas.
The second script: getHtmlSocialLinksByAuthor returns an html with an html list for displaying the social links based on the first script.
In short, having both scripts in your theme’s functions.php file, but calling the second script where you want the social links: getHtmlSocialLinksByAuthor.