WordPress – WordPress升级5.8之后获取最新评论的代码失效问题解决
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:WordPress – WordPress升级5.8之后获取最新评论的代码失效问题解决
原文链接:https://www.stubbornhuang.com/1568/
发布于:2021年08月14日 20:47:47
修改于:2021年08月14日 20:47:47

1 WordPress升级5.8之后获取最新评论的代码失效问题解决
手欠点了升级WordPress5.8之后今天发现主题中获取最新评论的功能失效了,之前主题中获取最新评论的代码如下:
/* 之前主题的旧代码,在升级为WordPress5.8之后失效 */
function suxingme_newcomments_old( $limit,$outpost,$outer ){
$limit = $limit ? $limit : 5;
$outpost = $outpost ? $outpost : 0;
$outer = $outer ? $outer : 1;
$output='';
global $wpdb;
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author,user_id, comment_date_gmt, comment_approved,comment_author_email, comment_type,comment_author_url, SUBSTRING(comment_content,1,40) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_post_ID!='".$outpost."' AND user_id!='".$outer."' AND comment_approved = '1' AND comment_type = '' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT $limit";
$comments = $wpdb->get_results($sql);
foreach ($comments as $comment) {
$output .= "<li><div class='message'><a href=\"" . get_permalink($comment->ID) ."#comment-" . $comment->comment_ID . "\" title=\"发表在: " .$comment->post_title . "\" class='comment_t'>". strip_tags($comment->com_excerpt)."</a></div><div class='clearfix meta'><div class='avatar'>".get_avatar( $comment )."</div><a href=\"" . get_permalink($comment->ID) ."#comment-" . $comment->comment_ID . "\" title=\" 在: " .$comment->post_title . "\" class='link'>".strip_tags($comment->comment_author)." 评 " .$comment->post_title . "</a></div></li>";
}
$output = convert_smilies($output);
echo $output;
};
经过在本地调试之后以及查询官方的文档之后,决定使用get_comments函数替换上述使用sql语句查询数据库获取最新评论的方式,修改后的代码如下:
/* 修改于2021年8月14日,修复在升级WordPress5.8之后最新评论显示失效的问题 */
function suxingme_newcomments( $limit,$outpost,$outer ){
$limit = $limit ? $limit : 5;
$output='';
$comments = get_comments('status=approve&number='.$limit.'');
foreach ($comments as $comment) {
$output .= "<li><div class='message'><a href=\"" . get_permalink($comment->ID) ."#comment-" . $comment->comment_ID . "\" title=\"发表在: " .$comment->post_title . "\" class='comment_t'>". strip_tags($comment->comment_content)."</a></div><div class='clearfix meta'><div class='avatar'>".get_avatar( $comment )."</div><a href=\"" . get_permalink($comment->ID) ."#comment-" . $comment->comment_ID . "\" title=\" 在: " .$comment->post_title . "\" class='link'>".strip_tags($comment->comment_author)." 评 " .$comment->post_title . "</a></div></li>";
}
$output = convert_smilies($output);
echo $output;
};
修改完成之后,获取最新评论的功能终于正常了!
当前分类随机文章推荐
- WordPress - 禁止非管理员登录后台 阅读1682次,点赞0次
- WordPress - count_user_posts函数,获取某个用户发表的文章数量 阅读708次,点赞0次
- WordPress - home_url()函数,获取网站主页url链接 阅读799次,点赞0次
- WordPress - 禁用XML-RPC接口,禁止访问xmlrpc.php,避免DDOS攻击,防止暴力破解 阅读2694次,点赞0次
- WordPress - $Post WP_Post对象的属性 阅读2400次,点赞0次
- WordPress - get_footer函数,加载主题底部页脚footer模板 阅读754次,点赞0次
- WordPress - 应对暴力破解登录和规避DDOS的几种方法 阅读2205次,点赞0次
- WordPress - admin_url()函数,获取网站管理后台url链接 阅读825次,点赞0次
- WordPress - 发送邮件很慢的解决办法 阅读516次,点赞1次
- WordPress - WordPress后台登录设置验证码,防止恶意爆破网站 阅读3783次,点赞0次
全站随机文章推荐
- Modern OpenGL - GLSL着色语言3:GLSL中的数据类型 阅读1841次,点赞0次
- 资源分享 - Computational Geometry - Algorithms and Applications, First Edition 英文高清PDF下载 阅读1607次,点赞0次
- 资源分享 - The Algorithms and Principles of Non-photorealistic Graphics - Artistic Rendering and Cartoon Animation 英文高清PDF下载 阅读1337次,点赞0次
- 资源分享 - Graphics Gems III 英文高清PDF下载 阅读2005次,点赞0次
- Youtube运营 - 如何使用一个Google账号创建多个Youtube频道 阅读189次,点赞0次
- WordPress - get_post_type():获取当前文章或者给定文章类型 阅读2013次,点赞0次
- 书籍翻译 – Fundamentals of Computer Graphics, Fourth Edition,第10章 Surface Shading中文翻译 阅读1364次,点赞3次
- 资源分享 - 白话深度学习与TensorFlow PDF下载 阅读2053次,点赞1次
- 书籍翻译 – Fundamentals of Computer Graphics, Fourth Edition,第3章 Raster Images中文翻译 阅读2032次,点赞3次
- 计算几何 - 求解两个三维向量之间的三维旋转矩阵 阅读1155次,点赞0次
评论
168