I’ve added the following code to my functions.php to expand the functionality of the user profile page and be able to add links to the social media of the authors.
function my_new_contactmethods( $contactmethods ) {
// Add Site
$contactmethods['mysite'] = 'My Site';
// Add Google Plus
$contactmethods['google_plus'] = 'Google Plus';
// Add Behance
$contactmethods['behance'] = 'Behance';
// Add Twitter
$contactmethods['twitter'] = 'Twitter';
//add Facebook
$contactmethods['facebook'] = 'Facebook';
return $contactmethods;
}
add_filter('user_contactmethods','my_new_contactmethods',10,1);
On the author.php I call the links like this:
<a title="Follow me on Twitter" href="<?php the_author_meta( 'twitter', $author_id ); ?>"><img src="twitter.png" alt="" /></a>
<a title="Follow me on Facebook" href="<?php the_author_meta( 'facebook', $author_id ); ?>"><img src="facebook.png" alt="" /></a>
<a title="Follow me on Behance" href="<?php the_author_meta( 'behance', $author_id ); ?>"><img src="behance.png" alt="" /></a>
<a title="Follow me on Google+" href="<?php the_author_meta( 'google_plus', $author_id ); ?>"><img src="google-plus.png" alt="" /></a>
The problem with this solution is that it shows the image and link even if the author has not filled the corresponding field.
Would it be some easy way to just echo the links that have been previously filled?
This is a very simple PHP issue, unrelated to WordPress. You need to check if the value is empty before
echo
ing it. So, store them as variables first:Then check them before displaying: