繁体   English   中英

WordPress:列出没有评论的帖子

[英]WordPress: List posts with no comments

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

您可以设置过滤器并查询变量以修改查询帖子的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' ); 

不幸的是,query_posts不允许您将查询限制为comment_count=0 你可以这样做:

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

但这不仅显示零注释的帖子,它只显示那些首先没有注释的帖子。

更复杂(但更好)的解决方案是使用自定义查询,专门将查询限制为具有0条评论的帖子,但这意味着您必须创建自己的循环结构(至少据我所知

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; ?>

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

很简单:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM