我做了一个功能,向登录用户显示她的所有评论,并允许她通过与打印的每个评论相关的删除按钮单独删除这些评论。该功能起作用,但在执行表单操作后,刷新时页面仍会显示已删除的注释,并且仅当您再次转到该页面(不是刷新,而是通过单击浏览器地址栏上的返回键)时,页面才会显示已批准的注释。如何解决此问题?
代码如下:
function custom_delete_post_comment() {
$comment_id = comment_ID();
wp_delete_comment( $comment_id, true ) ;
if ( isset( $_POST[\'comment_delete_nonce\'] ) ) {
if( wp_verify_nonce( $_POST[\'comment_delete_nonce\'], \'comment_delete_nonce\' ) ) {
set_query_var( \'commentid1\', $_POST[\'commentid\'] );
wp_delete_comment( get_query_var( \'commentid1\'), true );
}
}
}
function users_frontend(){
global $wpdb;
global $current_user;
if(is_user_logged_in()){
$result = $wpdb->get_results("SELECT * FROM wp_comments WHERE user_id = $current_user->ID");
$content = \'<div class="welcome"> Ciao, \'. $current_user->first_name .\'</div>\';
$content .= \'<h2>Questi sono gli eventi a cui ti sei iscritto. Per cancellare la singola iscrizione, clicca sulla "x" in alto a destra su ogni post</h2>\';
$content .= \'<div class="list-comment">\';
foreach($result as $row){
$content .= \'<div class="comment-recap">\';
$content .= get_the_post_thumbnail( $row->comment_post_ID );
$content .= \'<form method="POST" action="\'. custom_delete_post_comment() .\'" class="delete-comment-form">
<input type="hidden" name="comment_delete_nonce" value="\'. wp_create_nonce(\'comment_delete_nonce\') .\'" />
<input type="hidden" name="commentid" value="\'. $row->comment_ID .\'" />
<input type="submit" value="x" title="x" id="submit-btn" class="btn" />
</form>
<span><a href="\'. get_permalink( $row->comment_post_ID ) .\'">\'. get_the_title( $row->comment_post_ID ) .\'</a></span>
</div>\';
$content .= wp_get_comment_status( $row->comment_ID );
}
$content .= \'</div>\';
}
return $content;
}
echo users_frontend();
?>