JustNews主题评论调用排除博主的评论
justnews主题边栏小工具中,调用评论列表,默认是调用所有的评论(包含所有博主登录后回复的评论也会调用显示)。
只调用非博主的评论,你需要在评论查询中添加一个条件来排除博主的评论。通常,WordPress博主的评论具有user_id
,而其他评论的user_id
为0。所以添加 'user_id' => 0 条件以排除博主的评论条件到 justnews 主题 comments.php 文件中就可以实现排除博主的评论。
<?php
defined( 'ABSPATH' ) || exit;
class WPCOM_comments_widget extends WPCOM_Widget{
public function __construct(){
$this->widget_cssclass = 'widget_comments';
$this->widget_description = '显示网站最新的评论列表';
$this->widget_id = 'comments';
$this->widget_name = '#最新评论';
$this->settings = array(
'title' => array(
'name' => '标题',
),
'number' => array(
'value' => 10,
'name' => '显示数量',
)
);
parent::__construct();
}
public function widget( $args, $instance ) {
if ( $this->get_cached_widget( $args ) ) return;
ob_start();
$number = !empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['value'];
$comments_query = new WP_Comment_Query();
$comments = $comments_query->query( array( 'post_status' => 'publish', 'status' => 'approve', 'number' => $number, 'user_id' => 0 ) ); // 添加 'user_id' => 0 条件以排除博主的评论
$this->widget_start( $args, $instance );
if ( $comments ) : ?>
<ul>
<?php foreach ( $comments as $comment ) :
$author_url = $comment->comment_author_url ?: '#';
$display_name = $comment->comment_author;
$attr = 'target="_blank" rel=nofollow'; ?>
<li>
<div class="comment-info">
<a href="<?php echo esc_url($author_url);?>" <?php echo $attr; ?>>
<?php echo get_avatar( $comment, 60, '', $display_name ?: __('Anonymous', 'wpcom') );?>
<span class="comment-author"><?php echo $display_name ?: __('Anonymous', 'wpcom');?></span>
</a>
<span><?php echo date(get_option('date_format'), strtotime($comment->comment_date)); ?></span>
</div>
<div class="comment-excerpt">
<p><?php comment_excerpt( $comment );?></p>
</div>
<p class="comment-post">
<?php _e('Comment on', 'wpcom');?> <a href="<?php echo get_permalink($comment->comment_post_ID); ?>" target="_blank"><?php echo get_the_title($comment->comment_post_ID);?></a>
</p>
</li>
<?php endforeach;?>
</ul>
<?php
else:
echo '<p style="color:#999;font-size: 12px;text-align: center;padding: 10px 0;margin:0;">'.__('No comments', 'wpcom').'</p>';
endif;
$this->widget_end( $args );
echo $this->cache_widget( $args, ob_get_clean(), 3600 );
}
}
// register widget
function register_wpcom_comments_widget() {
register_widget( 'WPCOM_comments_widget' );
}
add_action( 'widgets_init', 'register_wpcom_comments_widget' );
panda 主题排除管理员评论
<?php
/*
/$$
/$$ /$$$$
| $$ |_ $$ /$$$$$$$
/$$$$$$$$ | $$ /$$_____/
|__ $$__/ | $$ | $$$$$$
| $$ | $$ \____ $$
|__/ /$$$$$$ /$$$$$$$/
|______/|_______/
================================
Keep calm and get rich.
Is the best.
*/
class Comments_List extends WP_Widget
{
/**
* Sets up the widgets name etc
*/
public function __construct()
{
$widget_ops = array(
'classname' => 'widget_comments_list',
'description' => '显示最新的评论。',
);
parent::__construct('Comments_List', 'Panda PRO - 最新评论', $widget_ops);
}
public function form($instance)
{
return $instance;
}
/**
* Outputs the content of the widget
*
* @param array $args
* @param array $instance
*/
public function widget($args, $instance)
{
if (!isset($args['widget_id'])) {
$args['widget_id'] = $this->id;
}
$widget_id = 'widget_' . $args['widget_id'];
$title = get_field('title', $widget_id);
$num = get_field('num', $widget_id) ?? '6';
$title = !empty($title) ? $title : __('Hot Authors', 'pandapro');
$title = apply_filters('widget_title', $title, $instance, $this->id_base);
echo $args['before_widget'];
if ($title) {
echo $args['before_title'] . $title . $args['after_title'];
}
// 获取所有管理员的用户 ID
$admin_ids = get_users(array(
'role' => 'administrator',
'fields' => 'ID',
));
$comment_args = array(
'number' => $num,
'status' => 'approve',
'type' => '',
'author__not_in' => $admin_ids, // 排除管理员评论
);
$comments = get_comments($comment_args);
if ($comments): ?>
<ul class="widget-comments">
<?php
foreach ($comments as $comment):
$comment_author = get_comment_author($comment);
$comment_content = get_comment_text($comment);
$comment_post_title = get_the_title($comment->comment_post_ID);
$comment_permalink = get_comment_link($comment);
$comment_user = get_user_by('email', $comment->comment_author_email);
$comment_date = pandapro_timeago(get_comment_date('G', $comment));
?>
<li class="item">
<?php
if ($comment_user):
$author_link = get_author_posts_url($comment_user->ID);
?>
<a href="<?php echo $author_link ?>" class="comment-avatar flex-avatar me-3" target="_blank">
<?php echo get_avatar($comment, 50) ?>
</a>
<?php else: ?>
<div class="comment-avatar flex-avatar me-3">
<?php echo get_avatar($comment, 50) ?>
</div>
<?php endif; ?>
<div class="comment-details">
<div class="comment-content">
<a href="<?php echo $comment_permalink ?>" class="d-inline-block bg-light rounded p-2" title="<?php echo $comment_post_title ?>">
<div class="h-3x">
<?php echo $comment_content ?>
</div>
</a>
</div>
<div class="comment-author text-xs mx-1 mt-1">
<?php
if ($comment_user):
$author_link = get_author_posts_url($comment_user->ID);
?>
<a href="<?php echo $author_link ?>" target="_blank">
<?php echo $comment_author ?>
</a>
<?php else: ?>
<?php echo $comment_author ?>
<?php endif; ?>
<span class="comment-date text-muted ms-2">
<?php echo $comment_date; ?>
</span>
</div>
</div>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
暂无评论
<?php endif; ?>
<?php
echo $args['after_widget'];
}
}
function reg_comments_list()
{
register_widget("Comments_List");
}
add_action('widgets_init', 'reg_comments_list');
这是wordpress的吗?
@老宋 是的,wordpress的。