duanji1899 2011-01-28 15:11
浏览 12
已采纳

WordPress:列出没有评论的帖子

I have simple page that I want to display a list of posts that haven't been commented on yet. How would I do this? I presume it's some parameters I can add to the query_posts? Thanks.

  • 写回答

3条回答 默认 最新

  • dstm2014 2011-01-28 18:25
    关注

    Unfortunately query_posts does not allow you to limit the query to comment_count=0. You can do this:

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

    But that does not only display posts with zero comments, it just displays those with zero comments first.

    The more involved (but better) solution is to use a custom query that specifically limits the query to posts with 0 comments, but that means you would have to create your own loop structure (at least 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; ?>
    

    Does that seem within your knowledge to implement?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染