【问题标题】:WordPress: List posts with no commentsWordPress:列出没有评论的帖子
【发布时间】:2011-06-17 07:34:06
【问题描述】:

我有一个简单的页面,我想显示尚未评论的帖子列表。我该怎么做?我认为这是我可以添加到 query_posts 的一些参数?谢谢。

【问题讨论】:

    标签: php wordpress comments


    【解决方案1】:

    简单到:

    query_posts( array ( 'post_type' => 'yourposttype', 'posts_per_page' => 10, 'comment_count' => 0, ) );
    

    【讨论】:

      【解决方案2】:

      您可以设置过滤器和查询变量来修改查询帖子的 SQL。将此添加到您主题的 functions.php 文件中

      function filter_comment_count( $sql ){
          global $wpdb;
          $comment_count = get_query_var( 'comment_count' );
      
          if( is_numeric($comment_count) )
              $sql .= $wpdb->prepare( " AND {$wpdb->posts}.comment_count = %d ", $comment_count );
      
          return $sql;
      }
      

      然后您可以拨打query_posts( 'comment_count=0' );(任何号码),您只需事先添加过滤器,

      add_filter( 'posts_where', 'filter_comment_count'  );
      

      在您拨打电话后,您可能还想删除过滤器。

      remove_filter( 'posts_where', 'filter_comment_count' ); 
      

      【讨论】:

      • 是的,你能做什么?谢谢:)
      • 太棒了。这有助于解决使用 $sql .= $wpdb->prepare( " AND {$wpdb->posts}.comment_status = 'open' "); 启用 cmets 查找帖子的问题。而是。
      【解决方案3】:

      很遗憾,query_posts 不允许您将查询限制为comment_count=0。你可以这样做:

      query_posts( 'orderby=comment_count&order=ASC' );
      

      但这并不显示 cmets 为零的帖子,它只显示 cmets 为零的帖子first

      涉及更多(但更好)的解决方案是使用自定义查询,该查询专门将查询限制为具有 0 cmets 的帖子,但这意味着您必须创建自己的循环结构(至少 so far as I can tell

      global $wpdb;
      $query = "
        SELECT *
        FROM {$wpdb->prefix}posts
        WHERE
        {$wpdb->prefix}posts.post_type = 'post'
        AND {$wpdb->prefix}posts.post_status = 'publish'
        AND {$wpdb->prefix}posts.comment_count = 0
        ORDER BY {$wpdb->prefix}posts.post_date
        DESC;
      ";
      
      $pageposts = $wpdb->get_results($query, OBJECT);
      
       <?php if ($pageposts): ?>
       <?php global $post; ?>
       <?php foreach ($pageposts as $post): ?>
       <?php setup_postdata($post); ?>
      
       <div class="post" id="post-<?php the_ID(); ?>">
       <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
          <?php the_title(); ?></a></h2>
          <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
          <div class="entry">
             <?php the_content('Read the rest of this entry »'); ?>
          </div>
          <p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  
          <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
       </div>
       <?php endforeach; ?>
       <?php else : ?>
          <h2 class="center">Not Found</h2>
          <p class="center">Sorry, but you are looking for something that isn't here.</p>
          <?php include (TEMPLATEPATH . "/searchform.php"); ?>
       <?php endif; ?>
      

      这似乎在您的知识范围内实施吗?

      【讨论】:

      • 我有一些使用 SQL 和原始 PHP 的经验,所以我想我可以理解它。我只是玩玩,看看我能不能让它工作。感谢您的精彩回答。
      猜你喜欢
      • 1970-01-01
      • 2019-09-24
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      • 2014-10-13
      • 2023-03-04
      • 2020-02-23
      • 1970-01-01
      相关资源
      最近更新 更多