ThaClown
Member
Posted 2 years ago #
Hi all I have used the Yoast method to add Twitter contact field in my user profiles. Works great.
Link: User Contact Fields in WordPress 2.9
But now I would like to show a Twitter user link only when the field is filled in... How can I do this?
I use
the_author_meta('twitter', $current_author->ID)
to show the link, but how can I make it only show the link if the field is filled in?
Code I use in functions.php:
function add_twitter_contactmethod( $contactmethods ) {
// Add Twitter
$contactmethods['twitter'] = 'Twitter';
// Remove Yahoo IM
unset($contactmethods['yim']);
return $contactmethods;
}
add_filter('user_contactmethods','add_twitter_contactmethod',10,1);
get_the_author_meta returns value instead of displaying. Use trim() so if the twitter data is blank but has spaces you make sure it counts as empty.
if(trim(get_the_author_meta('twitter', $current_author->ID) != ""){
the_author_meta('twitter', $current_author->ID);
}
ThaClown
Member
Posted 2 years ago #
Ok, but lets say I want a link (with a background image of a Twitter icon)
And it should only show if the twitter field is filled in.
I use this now:
<div class="post_twitter"><a href="<?php the_author_meta('twitter'); ?>" title="Twitter"><span>twitter</span></a></div>
CSS:
.post_twitter a {
width: 16px;
height: 16px;
background: url("images/site/twitter.png") no-repeat;
}
.post_twitter span {
display: none;
}
Could you help me a bit more?
Same way, everything inside the if statement that will show if filled in. But I split up the PHP so its easier to manage the HTML.
<?php if(trim(get_the_author_meta('twitter', $current_author->ID) != ""){ ?>
<div class="post_twitter"><a href="<?php the_author_meta('twitter'); ?>" title="Twitter"><span>twitter</span></a></div>
<?php } ?>
ThaClown
Member
Posted 2 years ago #