• Resolved anandg

    (@anandg)


    At Weblog Tools Collection they explained about using WP_Query:
    https://weblogtoolscollection.com/archives/2008/04/13/define-your-own-wordpress-loop-using-wp_query/

    So I tried it out. I installed WordPress 2.5 and created 4 test posts.
    Then I create a page template (news.php) and then created a page news which uses the template “news” (news.php).

    In the resulting page it only shows the date in the first post. In the other posts the date is empty. I don’t understand why.

    The code I’ve used is this:

    <h3>Recent Articles</h3>
            <?php
              $recentPosts = new WP_Query();
              $recentPosts->query('showposts=5');
            ?>
             <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
             <h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
             <?php the_date('F jS, Y', '<strong>', '</strong>'); ?><br />
             <?php the_content(); ?><br /><br />
             <?php endwhile; ?>

    I even changed the_date as described in this post at the bottom:
    https://www.ads-software.com/support/topic/136773?replies=5

Viewing 5 replies - 1 through 5 (of 5 total)
  • Make a habit to go to the Codex and READ about the template tags you are using. Template_Tags/the_date

    Thread Starter anandg

    (@anandg)

    Hi moshu,

    I always have a look at the WordPress documentation first and if I still don’t succeed then I do an extended search in Google.

    But it doesn’t work correctly. I use the following code:

    <h3>Recent Articles</h3>
    <?php $recentPosts = new WP_Query("cat=3&showposts=10"); ?>
    <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    <?php the_date('Y-m-d', '<strong>', '</strong>'); ?>
    <?php the_content(); ?>
    <?php endwhile; ?>

    It shows all the posts from the category ‘news’ wich has the ID 3. But not all the posts show a date. The last one shows a date. Then the next 2 don’t have a date, the 4th post does have a date, but the 5th and 6th don’t have a date.

    I can’t see where the problem lies. Should I be using a category template instead of a page template?

    You did not read the link that Moshu provided.

    You are using the_date

    Now go read the link that Moshu provided.

    SPECIAL NOTE: When there are multiple posts on a page published under the SAME DAY, the_date() only displays the date for the first post (that is, the first instance of the_date()). To repeat the date for posts published under the same day, you should use the Template Tag the_time() with a date-specific format string.

    Thread Starter anandg

    (@anandg)

    Ai, you’re right. I didn’t read the intro but I’ve read the rest. But in the intro was the part that I needed.

    Thanks for mentioning. I guess I have to read better.

    It now works. I’m using:
    <?php the_time('F j, Y'); ?> at <?php the_time('g:i a'); ?>

    yes, and thats overkill;

    the_time(‘F j, Y \a\t g:i a’);

    should work.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘the_date is empty in a loop’ is closed to new replies.