2013-08-03 50 views
0

我有一些数据只有在对帖子有一条或多条评论时才会显示。如果有0条评论,我想不显示任何内容。WP评论计数PHP有条件

的逻辑是这样的:

如果评论计数> 0 {做到这一点} 其他{}

如果有人可以帮助我用PHP代码段为这种类型的事情,将不胜感激。

回答

0
$nr_comments = get_comments_number($post_id); 

if($nr_comments > 0){ 
    //we have comments and do this 
} 
else{ 
    //we don't have any comments yet and do something else 
} 

注意:该标签必须在循环内。

由于这是在循环中,你不需要$post_id

+0

我可能需要在循环外运行,也许在自定义查询中,是否有另一种方法我应该使用它? – user1447958

+0

如果在循环之外给它一个帖子ID,它应该是它的工作。 –