WordPress根据最后一次评论时间隐藏评论者链接

本文将介绍依据评论者最后一次评论时间来决议能否显现评论者链接的实现方法,比如评论者的最后一次评论时间离现在超过30天,就制止显现评论者网站链接,此方法可用于加强对评论的控制。

  • 将以下代码添加到正在使用主题的 functions.php 文件内:
/**
* wordpress根据评论者最后一次评论时间来决定是否显示评论者链接
*/
function lxtx_show_comment_author_link( $return , $author, $comment_ID ){
if ( !is_user_logged_in() ) { // 登录用户无鸭梨
date_default_timezone_set('PRC');
$limit_days = 10; // 天数,代表最后一次评论时间距离今天超过10天的话,则隐藏评论链接
$comment = get_comment( $comment_ID );
if ( !empty($comment->comment_author_email) ) {
$last_comment = get_comments(array(
'author_email' => $comment->comment_author_email,
'number' => '1',
'orderby' => 'comment_date',
'order' => 'DESC',
'status' => 'approve',
'type' => 'comment'
));
if ( (time() - strtotime($last_comment->comment_date)) > $limit_days*24*3600 ) {
$return = $author;
}
}else{
$return = $author;
}
}
return $return;
}
add_filter('get_comment_author_link','lxtx_show_comment_author_link', 10, 3);

*仅适用于 the_author_link()来输出评论者昵称的主题。

*$limit_days = 10;的参数按需修改,这里的10为距离当天多少天。

  • 第二种:
// 评论链接跳转内链
function commentauthor($comment_ID = 0) {
$comment_temp = get_comment( $comment_ID );
$author = $comment_temp->comment_author;
$url = $comment_temp->comment_author_url ;
$email = $comment_temp->comment_author_email;
$cmuserid = $comment_temp->user_id;
$limit_days = 30; // 天数,最后一次评论时间距离今天超过 $limit_days 天则隐藏评论链接
if ( $cmuserid > 0 ) {
echo "$author";
} else if ( empty( $url ) || 'http://' == $url ) {
echo $author;
} else if ( !empty( $email ) && !is_user_logged_in() ) {
date_default_timezone_set('PRC'); //时区
$last_comment = get_comments(array(
'author_email' => $email,
'number' => '1',
'orderby' => 'comment_date',
'order' => 'DESC',
'status' => 'approve',
'type' => 'comment'
));

if ( ( current_time('timestamp') - strtotime( $last_comment['0']->comment_date ) ) > $limit_days*24*3600 ) {
echo "$author";
} else {
echo "$author";
}
} else {
echo "$author";
}
}

需配合主题修改。

《WordPress根据最后一次评论时间隐藏评论者链接》版权归逐凨所有
转载请保留原文链接:https://izhizu.com/wordpress/104.html
THE END
分享
二维码
< <上一篇
下一篇>>