刚写了个新的回复可见的区块,UI随便在网上找的,不过这个判断文章是否被评论还是麻烦了我半天,最后还是在老博客上找到了以前发的代码,修改下后就可以直接使用,理论上支持「WP 5.X
」以上的版本。
![WordPress判断文章是否被评论代码插图 WordPress判断文章是否被评论代码插图](https://static.eswlnk.com/2023/01/20230130001703468.png-esw)
<?php
// Created by XiaoWu
// $post_id 当前文章id,可通过全局变量获取
// 返回是否被当前用户评论
function is_comment( $post_id ) {
$email = null;
$user_ID = wp_get_current_user()->ID;
$user_name = wp_get_current_user()->display_name;
if ( $user_ID > 0 ) {
$email = get_userdata( $user_ID )->user_email;
} else if ( isset( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) {
$email = str_replace( '%40', '@', $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] );
} else {
return false;
}
if ( empty( $email ) && empty( $user_name ) ) {
return false;
}
global $wpdb;
$pid = $post_id;
$query = "SELECT `comment_ID` FROM {$wpdb->comments} WHERE `comment_post_ID`={$pid} and `comment_approved`='1' and (`comment_author_email`='{$email}' or `comment_author`='{$user_name}') LIMIT 1";
if ( $wpdb->get_results( $query ) ) {
return true;
}
}
将以上代码复制到你的博客主题或者集成插件中,参数为文章的id,可以使用WordPress内部的id全局变量,返回当前文章是否被当前用户评论。
📮评论