【发布时间】:2013-12-06 19:16:51
【问题描述】:
我有一个自定义模板,它使用循环来获取帖子信息以构建自定义滑块,它运行良好。客户提出了一个新要求,即他们只希望过去 2 天的帖子显示在滑块中。
在循环中我添加了以下 if 语句:
$today = date('j');
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
// Added the below code
if (the_date('j','','',FALSE)>($today-3)) :
// Builds slide
endif;
// End of new code
endwhile;
else:
?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php
endif; ?>
添加此代码后,循环仅显示单个帖子,即找到满足此 IF 语句条件的第一个帖子,不显示任何其他帖子。我手动更改了日期,以便在过去两天内至少有 3 个帖子具有发布日期,仍然没有。
当我取消注释上述行时,代码可以正常工作,并将所有帖子拉到幻灯片中。
我不能使用“posts_where”过滤器,因为这会影响页面上的其他帖子(想象一下页面顶部的滑块和滑块下方的帖子)。
我有什么遗漏吗?还是不能这样用the_date()?
【问题讨论】:
标签: wordpress date loops slider posts