0

I have a script that I have somehow managed to create (since my knowledge of php is very limited) that basically places author's profile (image, bio, and links to social media) on to authors archive page with all the posts by same author.

the problem with this script is it returns empty href's and src's in the div's if inputs in the authors profile are empty. I understand the reason why it does this, but I do not know how to correct it.

the second problem is if author set up a custom image link that my script returns that image, but what also like to do in case if input for that link is empty is to return an avatar.

Here is my script

    /***********************************************************
******************* Custom Author Page *********************
***********************************************************/
function author_info() {
if (is_author($avatar, $id_or_email, $size, $default, $alt)) {
 if(get_query_var('author_name'))
      $author = get_userdatabylogin(get_query_var('author_name'));
      $authorID = $author->ID;
      $custom_avatar = get_the_author_meta('imageurl', $authorID);
    if ($authorID=="1")
    echo ' ';
    else 
 echo '<div class="author_profile author_'. $authorID .'">
        <div class="author_avatar"><img alt="Author" src="'. $custom_avatar .'" width="140" height="140" /></div>
            <div class="author_info"> 
                <div class="author_description">
                <p>'. get_the_author_meta('description', $authorID) .'</p> 
                <p><strong>Author\'s Website:</strong><a href="'. get_the_author_meta('user_url', $authorID) .'">'. get_the_author_meta('wesitename', $authorID) .'</a></p>
                </div>
            </div>
            <div class="author_links">
            <a href="https://plus.google.com/'. get_the_author_meta('google_plus', $authorID) .'" rel="me" target="_blank"><img alt="Google Plus Profile" src="'. get_the_author_meta('google_plus_icon', $authorID) .'" width="128" height="64" /></a>
            <a href="http://www.facebook.com/'. get_the_author_meta('facebook', $authorID) .'" target="_blank"><img alt="Facebook Page" src="'. get_the_author_meta('facebook_icon', $authorID) .'" width="128" height="64" /></a>
            <a href="http://twitter.com/'. get_the_author_meta('twitter', $authorID) .'" target="_blank"><img alt="Twitter Page" src="'. get_the_author_meta('twitter_icon', $authorID) .'" width="128" height="64" /></a>
            <a href="http://www.linkedin.com/company/'. get_the_author_meta('linkedin', $authorID) .'" target="_blank"><img alt="LinkedIn Profile" src="'. get_the_author_meta('linkedin_icon', $authorID) .'" width="128" height="64" /></a>
            </div>
       </div>
       ';
  ?>
<?php }
}
add_action('thesis_hook_before_content', 'author_info');

Your help is highly appreciated.

4

1 回答 1

0

您可以更改函数: get_the_author_meta($type, $id) 以返回默认值。

get_the_author_meta($type, $id) {
    **Code to get value ** 
    if(isset($value)) {
        return $value;
    } else {
        return "#"; // Or some other string
    }
}

但是,此解决方案将意味着链接仍将显示。如果您根本不希望链接显示,您可以执行以下操作:

if(isset(get_the_author_meta('google_plus', $authorID)) && isset(get_the_author_meta('google_plus_icon', $authorID))) :
<a href="https://plus.google.com/'. get_the_author_meta('google_plus', $authorID) .'" rel="me" target="_blank"><img alt="Google Plus Profile" src="'. get_the_author_meta('google_plus_icon', $authorID) .'" width="128" height="64" /></a>
endif;

然后只会显示具有 href 和 img 的链接。

对于您的头像,您可以返回一个默认值。在你的代码中找到去的地方get_the_author_meta('imageurl', $authorID); 。可能是像 getImageUrl() 这样的函数。在该函数中执行与以前类似的操作:

getImageUrl() {
    **Code to get url ** 
    if(isset($value)) {
        return $value;
    } else {
        return "http://www.mysite.com/img/base-avatar.png"; // Or some other base image
    }
}
于 2013-01-14T13:01:07.863 回答