add_filter( 'get_avatar' , 'alt_name_avatar');
function alt_name_avatar( $avatar ) {
$alt = get_comment_author();
$avatar = str_replace('alt=\'\'','alt=\'Avatar for '.$alt.'\' title=\'Avatar for '.$alt.'\'',$avatar);
return $avatar;
}
此代码工作正常,但会引发错误。
PHP Notice: Trying to get property 'user_id' of non-object in .../wp-includes/comment-template.php on line 28
PHP Notice: Trying to get property 'comment_ID' of non-object in .../wp-includes/comment-template.php on line 48
如何修复。
P.S.我在所有页面上都使用最近在侧栏中使用Gravatar的注释
对不起我的英语。
发布于 2021-06-04 15:33:52
在使用get_comment_author();
时,您不会检查正在查看评论的任何位置。get_avatar()
函数在在WordPress的很多地方中使用;您的代码似乎假定它仅用于注释。
试试这个(代码还没有经过测试,但我认为应该有效):
add_filter( 'get_avatar' , 'alt_name_avatar');
function alt_name_avatar( $avatar ) {
if ( null === get_comment() ) {
// This isn't a comment.
return $avatar;
}
$alt = get_comment_author();
$avatar = str_replace('alt=\'\'','alt=\'Avatar for '.$alt.'\' title=\'Avatar for '.$alt.'\'',$avatar);
return $avatar;
}
似乎没有一个简单的is_comment()
检查来查看我们是否正在查看评论,所以我选择测试get_comment()
,如果我们不在注释中,它将返回null
。
https://wordpress.stackexchange.com/questions/390129
复制相似问题