Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using wordpress 3.2 and I did a query post like this:

<?php query_posts("posts_per_page=1post=type&page=post_parent=10");?>

Then I try to echo out the date of this post I queried like this.

<?php echo the_date(); ?>

It gives me the title of the post and the excerpt and the permalink but no date. What do you think the problem is. I'm sure it's something quite embarrassing.

Here is the code in my template file for the video page:

    <?php query_posts("posts_per_page=1post=type&page=post_parent=10");?>
    <h2>Recent Video</h2>
    <h3 class="date"><?php echo the_date(); ?></h3>
    <p><strong><?php echo the_title(); ?></strong><?php echo the_excerpt(); ?></p>
    <p><a href="<?php echo the_permalink(); ?>" class="more2">Watch Now</a></p>

Here I try to put the query in a loop:

<?php query_posts("posts_per_page=1post=type&page=post_parent=10");?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2>Recent Video</h2>
<h3 class="date"><?php echo the_date(); ?></h3>
<p><strong><?php echo the_title(); ?></strong><?php echo the_excerpt(); ?></p>
<p><a href="<?php echo the_permalink(); ?>" class="more2">Watch Now</a></p>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

the_date() did not work but the the_title() and other functions worked. By the way this changed my query to the_post() which is not what I'm wanting. I want to query the latest video like I did above the loop.

By the way I used the_date function earlier in the page and it worked. Could that be the problem? Here is it before the code that I had a problem with.

<div id="col75" class="firstcol">
    <iframe id="video" src="http://www.youtube.com/embed/videoseries?list=<?php print get_post_meta($post->ID,"playlist_id", true); ?>" width='560' height='350' frameborder="0"></iframe>
    <div id="col25">
        <h2><?php echo get_post_meta($post->ID,"speaker", true); ?></h2>
        <h3 class="date"><?php echo the_date(); ?></h3>
share|improve this question
    
Can you post your loop initialization code? –  m0r7if3r May 17 '12 at 21:17
    
I edited the post and put the code I used for the template file. –  zachdyer May 17 '12 at 21:19
    
You have much to learn young padawan. –  m0r7if3r May 17 '12 at 21:21
add comment

migrated from stackoverflow.com May 17 '12 at 21:09

This question came from our site for professional and enthusiast programmers.

4 Answers

up vote 14 down vote accepted

See this special note about using the `the_date'

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() or get_the_date() (since 3.0) with a date-specific format string. Use to add the date set in the admin interface.

  1. Your using query_posts which screws up the globals
  2. Your echoing a function that already prints to the browser

    • Your actually doing that for all your template tags.
    • Change echo the_date(); to: echo get_the_date('F j, Y');
    • Remove the echo from your template tags that already print to the browser or use the alternate functions that return the value.
  3. Use a new WP_Query or get_posts instead of query_posts

  4. Read the Codex. It tells you how to use all these functions and is very helpful :)

share|improve this answer
    
get_the_date() fixed my problem but now my sidebar.php get_the_date() is broken. –  zachdyer May 17 '12 at 22:06
    
Another victim of query_posts –  Chris_O May 17 '12 at 22:07
    
Okay it's working now but I had to used the echo tags. That is why it didn't work in my sidebar.php. –  zachdyer May 17 '12 at 22:22
add comment

the_date() prints the date only if the same date was not printed before.
No, that's not consistent with other similar functions. But that’s how it worked in WordPress’ ancestor b2/cafelog, and backwards compatibility always trumps logic … :)

To print the date always use get_the_date()

<?php echo get_the_date(); ?>

or

<?php echo mysql2date( get_option( 'date_format' ), $post->post_date); ?>
share|improve this answer
1  
Toscho, I bow before you in humble gratitude and gratefulness combined with joy in my heart for my salvation this day has come to a new age of toschoness. –  zachdyer May 17 '12 at 21:36
    
NOOOOOOOOOOOOOOOOOOOO. Now my side bar get_the_date is broken. What happen there? –  zachdyer May 17 '12 at 22:00
    
See Cris_O's answer and don't use query_posts(). :) –  toscho May 17 '12 at 22:04
    
Your Using query_posts which is breaking your globals. See my answer below. –  Chris_O May 17 '12 at 22:04
add comment

I think that is meant to be run within the while( have_posts() ) conditional:

while ( have_posts() ) : the_post();
    echo '<li>';
    the_date();
    echo '</li>';
endwhile;
share|improve this answer
    
How do I just say the date of the post? That's all I want to do? I only queried one post. –  zachdyer May 17 '12 at 21:12
add comment

You need to initialize the loop for certain functions to work. All of these functions list, on their codex page, that they will not function properly outside of the loop.

share|improve this answer
    
No that doesn't work I posted the code of what I did in the post. –  zachdyer May 17 '12 at 21:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.