2012-08-27 69 views
0

我一直在寻找网络我甚至试图聘请自由职业者在这方面的帮助,但没有运气。虽然搜索我发现这how to get popular posts fro selected categories in wordpress? & http://www.queness.com/code-snippet/6546/how-to-display-most-popular-posts-from-a-specific-category-in-wordpress这基本上是我想要的,但我想从我得到的信息分裂,所以我可以排名帖子。如何拆分Wordpress查询?

<?php 
$args=array(
    'cat' => 3, // this is category ID 
    'orderby' => 'comment_count', 
    'order' => 'DESC', 
    'post_type' => 'post', 
    'post_status' => 'publish', 
    'posts_per_page' => 6, // how much post you want to display 
    'caller_get_posts'=> 1 
); 
$my_query = null; 
$my_query = new WP_Query($args); 
if($my_query->have_posts()) { ?> 
<ul> 
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?> 
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> 
<?php endwhile; ?> 
</ul> 
<?php } 

wp_reset_query(); ?> 

与该代码由它得到的意见是最热门的职位和我想要做的基本上是拿结果和秩添加到它像下面的例子是什么。

#1 - post 1 
#2 - post 2 
#3 - post 3 
#4 - post 4 
#5 - post5 last post 

在此先感谢您的帮助

回答

1

可能是这样的想法会帮助你。

使用get_comments_number($ POST_ID)function

要得到评论的数量,然后做了排名显示,如果一个其他循环。

$num_comments = get_comments_number(); // get_comments_number returns only a numeric value 

if (comments_open()) { 
if ($num_comments == 0) { 
    $rating= 0 ; 
} elseif ($num_comments > 1) { 
    $rating= 1 ; 
} else { 
    $rating= 0 ; 
} 
} 

感谢

+0

我想比较每个帖子的评论,谢谢/ – leanswag

+0

是在循环中使用它,以便这将运行在每个帖子 – Maidul

0

通过当前的问题,我了解以下内容:

  1. 你想从具有最多评论的WP数据库的帖子进行查询。
  2. 您想向访问者显示收到的帖子的排名。排名由评论发布的数量决定。

所以,你的结果可能是:

1后A(评论数500)

2张贴B(评论数499)

3张贴Z(评论数200)

这是我该怎么做:

<?php 
function get_popular_posts() 
{ 

$sql="SELECT comment_count, guid AS link_to_post, post_title 
FROM wp_posts 
WHERE post_status = "publish" AND post_type = "post" 
ORDER BY comment_count DESC 
LIMIT 5" 
return $wpdb->get_results($sql, OBJECT) 

} 
$post_objects = get_popular_posts(); 
$i = 1; 
foreach($post_objects as $object) 
{ 
echo 'Post: ' . $i . '#' . ' ' . $post_title . ' ' . $link_to_post . ' ' . $comment_count; 
$i++; 
} 
?> 

未测试代码。但它应该从数据库中取得五个“最高职位”。出于解释的原因留在comment_count中。