[教程] 在循环外调用WordPress文章评论数

查看: 392|回复: 0
wpocn 发表于 2023-3-2 10:01:51
在循环外调用WordPress文章评论数。
  1. function add_comment_count_to_posts() {
  2.         global $wpdb, $post;
  3.         $id = $post->ID;// 文章ID
  4.         $comment_count = $wpdb->get_var( "SELECT comment_count FROM $wpdb->posts WHERE post_status = 'publish' AND ID = $id" );// 获取文章评论数
  5.         $comment_status = $wpdb->get_var( "SELECT comment_status FROM $wpdb->posts WHERE ID = $id" );// 文章是否开启了评论
  6.         $html = '<div class="comment-popup">';
  7.         $html .= '<span>';
  8.         $count = '';
  9.         if( $comment_status == 'open' ) {
  10.                 $count = $comment_count;
  11.         } else {
  12.                 $count = '评论关闭';
  13.         }
  14.         $html .= $count . '</span></div>';
  15.         return $html;
  16. }
复制代码
获取某文章最新一条评论日期:
  1. $comment_args = array(
  2.         'post_id'   => get_the_ID(),
  3.         'number'    => 1,
  4. );

  5. $latest_comment = get_comments($comment_args);
  6. echo $latest_comment[0]->comment_date;
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于本站联系我们FAQ友情链接免责声明生存法则

Build with for "make" Copyright © 2020-2022. Powered by Discuz! GMT+8, 2024-4-27 03:43

快速回复 返回顶部 返回列表