views:

56

answers:

1

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;?>
+1  A: 

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
BoltClock
You sir, are awesome, it sounds like you have hit the nail on the head. I will give it a try now. Thanks!
danixd
I added the unset previous day to the loop and it worked a treat. Thank you for uncovering that mystery!
danixd
You're welcome :)
BoltClock