[resolved] Only show Twitter profile info if filled in.... (5 posts)

  1. ThaClown
    Posted 6 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
    return $contactmethods;
  2. Darrell Schauss
    Posted 6 years ago #

    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);
  3. ThaClown
    Posted 6 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>


    .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?

  4. Darrell Schauss
    Posted 6 years ago #

    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 } ?>
  5. ThaClown
    Posted 6 years ago #

    THx alot

Topic Closed

This topic has been closed to new replies.

About this Topic