【发布时间】:2015-09-24 07:07:00
【问题描述】:
感谢 Rene Korss 几乎帮助解决了问题,并且每次都在讨论中回复。
我想检查发表评论的用户是否有头像?如果用户有头像(意味着拥有 gravatar 帐户),则显示 gravatar 头像,否则显示我在其他部分定义的图像。任何帮助都将不胜感激。
我目前正在使用此代码:
if(get_avatar()==1 )
{
echo get_avatar($comment,$size='48',$default='<path_to_url>' );
}
else
{ ?>
<img src="<?php bloginfo('template_directory'); ?>/img/admin.jpg" alt=""><?php
} ?>
此代码的输出只是其他部分工作。如果我将条件写为if(get_avatar()),那么只有部分工作。
$comment 具有值:
stdClass Object (
[comment_ID] => 9
[comment_post_ID] => 104
[comment_author] => Navnish
[comment_author_email] => ask@navnishbhardwaj.com
[comment_author_url] =>
[comment_author_IP] => 118.146.54.35
[comment_date] => 2015-09-23 14:33:11
[comment_date_gmt] => 2015-09-23 14:33:11
[comment_content] => this is comment by Admin
[comment_karma] => 0
[comment_approved] => 1
[comment_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0
[comment_type] =>
[comment_parent] => 0
[user_id] => 1
)
使用此代码时:
<?php
$avatar = get_avatar( get_the_author_meta( 'ID' ), $size = '48', $default = bloginfo( 'template_directory' ).'/img/admin.jpg' );
if( $avatar !== false )
{
echo $avatar;
}
?>
得到这样的输出:
输出:
$avatar = get_avatar( $comment->comment_author_email, $size = '48', $default = bloginfo( 'template_directory' ).'/img/admin.jpg' );
if( $avatar !== false )
{
echo $avatar;
}
【问题讨论】:
-
告诉我们你是如何得到
$comment的。 -
$comment 是 stdClass 对象。我已经更新了相关 $comment 的值
标签: php wordpress wordpress-theming