簡體   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