WordPress设置好评并输出
一些评论比较多的媒体站,管理员会把一些用户写的好评单独输出在侧边栏,但是怎么设置好评和输出呢?我们可以在 WordPress 后台评论管理界面评论管理区添加一个新的好评动作,并且通过 WP_Comment_Query 这个 Class 输出所有好评。
详细代码如下:
<?php add_filter('comment_row_actions','wpjam_good_comments_comment_row_actions',10,2); function wpjam_good_comments_comment_row_actions($actions, $comment ){ if(get_comment_meta( $comment->comment_ID, 'good_comment',true) == '1'){ $actions['good_comment'] = '<a href="'.home_url('/wp-admin/edit-comments.php?good_comment=0&comment_id='.$comment->comment_ID).'">取消好评</a>'; }else{ $actions['good_comment'] = '<a href="'.home_url('/wp-admin/edit-comments.php?good_comment=1&comment_id='.$comment->comment_ID).'">好评</a>'; } return $actions; } add_action('admin_head','wpjam_good_comments_admin_head'); function wpjam_good_comments_admin_head(){ if(isset($_REQUEST['good_comment'])){ if($_REQUEST['good_comment'] == '1'){ add_comment_meta( $_REQUEST['comment_id'], 'good_comment', '1'); }elseif($_REQUEST['good_comment'] == '0'){ delete_comment_meta( $_REQUEST['comment_id'], 'good_comment'); } } } function wpjam_get_good_comments(){ $comments_query = new WP_Comment_Query; $comments = $comments_query->query( array( 'meta_key' => 'good_comment', 'meta_value' => '1', 'number' => 10 ) ); if ( $comments ) { foreach ( $comments as $comment ) { $post = get_post($cooment->comment_post_ID); echo ' <li> '.get_avatar($comment->comment_ID,50).' <a href="'.get_permalink($comment->comment_post_ID).'" title="'. esc_attr(strip_tags($post->post_title)).'"><strong>@'.$comment->comment_author.'</strong>:'.get_comment_text($comment->comment_ID).'</a> </li> '; } } }
将上面插件单独保存为一个插件,并上传激活,然后既可以使用 wpjam_get_good_comments 模板函数输出最新的10条好评了。
原文链接:https://www.qiquanji.com/post/8054.html
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
微信扫码关注
更新实时通知