在循环外调用WordPress文章评论数

知更鸟 WordPress1 175阅读模式

在循环外调用WordPress某文章评论数

function add_comment_count_to_posts() {
	global $wpdb, $post;
	$id = $post->ID;// 文章ID
	$comment_count = $wpdb->get_var( "SELECT comment_count FROM $wpdb->posts WHERE post_status = 'publish' AND ID = $id" );// 获取文章评论数
	$comment_status = $wpdb->get_var( "SELECT comment_status FROM $wpdb->posts WHERE ID = $id" );// 文章是否开启了评论
	$html = '<div class="comment-popup">';
	$html .= '<span>';
	$count = '';
	if( $comment_status == 'open' ) {
		$count = $comment_count;
	} else {
		$count = '评论关闭';
	}
	$html .= $count . '</span></div>';
	return $html;
}

获取某文章最新一条评论日期:文章源自知更鸟-https://zmingcx.com/loop-out-the-number-of-wordpress-post-comments.html

$comment_args = array(
	'post_id'   => get_the_ID(),
	'number'    => 1,
);

$latest_comment = get_comments($comment_args);
echo $latest_comment[0]->comment_date;

 文章源自知更鸟-https://zmingcx.com/loop-out-the-number-of-wordpress-post-comments.html 文章源自知更鸟-https://zmingcx.com/loop-out-the-number-of-wordpress-post-comments.html

weinxin
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
评论  1  访客  1
    • 愛羅导航
      愛羅导航 0

      拿走了 :razz:

    匿名

    发表评论

    匿名网友 填写信息

    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

    确定