【问题标题】:WordPress the_date within the loop only returning one post循环中的 WordPress the_date 只返回一篇文章
【发布时间】: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


    【解决方案1】:

    我遇到了同样的问题,尝试使用

    <?php the_time(); ?>
    

    【讨论】:

    • 抱歉,我以为我可以正常工作,但仍然出现问题,the_time() 不显示任何内容,而是显示一堆数字
    【解决方案2】:

    这可能是您的问题吗?根据http://codex.wordpress.org/Template_Tags/the_datethe_date 文档中的“特别说明”:

    特别注意:当同一天发布的页面上有多个帖子时,the_date() 仅显示第一个帖子的日期(即 the_date() 的第一个实例)。要为同一天发布的帖子重复日期,您应该使用带有特定日期格式字符串的模板标签 the_time() 或 get_the_date()(自 3.0 起)。 用于添加在管理界面中设置的日期。

    所以请使用get_the_date,如此处所述:http://codex.wordpress.org/Template_Tags/get_the_date

    例如

    if (get_the_date('j') &gt; ($today-3)) :

    等等

    【讨论】:

    • 这对我来说非常有效,我不得不使用 the_time() 添加:
    【解决方案3】:

    不要问为什么,如果你能解释为什么请在cmets做,但不得不做这样的事情:

    $posts_date = $post->post_date;
    $two_days_ago = strtotime(date('Y-m-d H:i:s') . ' -2 day');
    if ($posts_date >(date('Y-m-d H:i:s', $two_days_ago))) : 
        //process code
    endif;
    

    【讨论】:

      【解决方案4】:

      这对我有用 the_time('d/m/Y');

      【讨论】:

        【解决方案5】:

        使用获取日期函数。

        <?php echo get_the_date(); ?>
        

        为什么 the_date 不工作但 get_the_date 工作?

        https://wordpress.stackexchange.com/questions/166751/post-doesnt-show-date-if-theres-another-post-with-the-same-date

        从上面的链接中查看 kaiser 的回答。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-09-01
          • 2018-04-03
          • 2013-07-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多