admin管理员组文章数量:1290954
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;
}
This code works but throws an error.
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
How to fix.
P.S. i use on all pages recent comments with Gravatar in the sidebar
Sorry for my English.
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;
}
This code works but throws an error.
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
How to fix.
P.S. i use on all pages recent comments with Gravatar in the sidebar
Sorry for my English.
Share Improve this question asked Jun 4, 2021 at 14:52 UserUser-nameUserUser-name 577 bronze badges1 Answer
Reset to default 1You don't check anywhere that you're viewing a comment when you use get_comment_author();
. The get_avatar()
function is used in a lot of places in WordPress; your code seems to assume it's only in use on comments.
Try this (the code is untested, but should work, I think):
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;
}
There doesn't seem to be a simple is_comment()
check to see if we're viewing a comment, so I've chosen to test get_comment()
, which will return null
if we're not in a comment.
本文标签: phpFix error Gravatar alt
版权声明:本文标题:php - Fix error Gravatar alt 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741522314a2383261.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论