开发者

WordPress the_date skipping at intervals inside a loop

I have made a custom loop in WordPress and for some reason, the date is being skipped on random intervals, even though the other post content is getting pulled in successfully every time.开发者_开发技巧

Any ideas, because it is completely baffling me!

For example, a list of posts and when the date is missing:

  1. Date
  2. Date
  3. No date
  4. Date
  5. Date
  6. No date
  7. Date
  8. No date
  9. No date
  10. No date

Here is the code, including all the loops:

    <?php query_posts('category_name=News&posts_per_page=10'); ?>
    <?php while (have_posts()) : the_post(); ?> 

    <article>
        <div>
            <p>PUBLISHED: <?php the_date(); ?></p>
            <h4><a class="news_title_link" href="<?php the_permalink();?>"><?php the_title();?></a></h4>
            <?php the_excerpt(); ?>
            <br />
            <a href="<?php the_permalink();?>">Read more</a>
        </div>  
        <div>
        <?php if ( function_exists( 'get_the_image' ) ) { get_the_image(array('default_size' => 'thumbnail','default_image' => '/wp-content/uploads/2010/06/default-thumb.jpg'));} ?>
        </div>
    </article>

    <?php endwhile; ?>
    <?php endif;?>


One likely cause could be that the consecutive posts with no dates are all published on the same day as the one with a date that they all immediately follow.

In your example, the second and third posts may have the same publish date, which is causing the third post to not display a date. Likewise, posts 7 to 10 may share the same publish date, which is causing the last three posts to not display dates.

That, as far as I've experienced, is how the_date() works. It prints a unique date only once throughout a loop.

I work around it by either:

  • Using the_time() instead of the_date() and specifying a full date format, or
  • Calling unset($previousday) just after my the_post() call


Both Wordpress' the_date() document and https://wordpress.stackexchange.com/questions/47190/date-not-appearing-in-custom-query suggest this.

<?php the_time(get_option('date_format')); ?>


As of 2017, there is another simpler solution. You can simply use:

echo get_the_date();

instead of the_date();. For me, it was the exact same problem with dates only displaying if they are different from the date before.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜