WordPress:显示"更多帖子"仅当帖子数量>按钮时3

时间:2016-02-09 17:52:04

标签: php wordpress

更新我实际上是在尝试显示更多帖子"按钮,仅当"新闻"中有超过3个帖子时类别。我在搜索Stack Overflow之后提出的当前代码如下......但似乎没有正常运行:

<?php
$cat = get_query_var('cat');
$posts = get_posts(array('News' => $cat));
if(count($posts) > 3)
{
 echo('<div class="more-box"><a href="#">More news and events</a></div>');
}
else
{
//CODE EXECUTED IF LESS THAN THREE POSTS EXIST IN CURRENT CATEGORY
}
?>

只有在新闻中有&gt; 3个帖子时才会显示更多按钮?

谢谢!

1 个答案:

答案 0 :(得分:1)

您的代码在没有该行的情况下正常显示:

$posts = get_posts(array('News' => $cat));

功能get_posts没有参数News。如果$cat category 的首字母缩写词,那么你应该有这样的东西:

$posts = get_posts(array('category' => $cat));

然后Wordpress应该返回WP_Post个对象的列表。另外,要计算帖子,你可以使用另一个内部功能:

 <?php wp_count_posts( $type, $perm ); ?> 

其中:

$type(字符串)(可选)要计数的帖子类型

$perm(字符串)(可选)要包含当前用户可读的私人帖子,请设置为“可读” - 默认值:空字符串

了解详情:wp_count_posts()get_posts()