Empty Date Returned on my function

I am using this function to display the date and title of my latest posts on my dashboard.

However for some of the posts the date is not being displayed at all – I have a feeling this might be the posts which I subsequently hit the “update” button but i am not sure.. any ideas why this happens?

function wps_recent_posts_dw() {
?>
   <ol>
     <?php
          global $post;
          $args = array( 'numberposts' => 10, 'post_type' => 'any' );
          $myposts = get_posts( $args );
                foreach( $myposts as $post ) :  setup_postdata($post); ?>
                    <li> (<? the_date('j M, Y @ G:i'); ?>) <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
          <?php endforeach; ?>
   </ol>
<?php
}

function add_wps_recent_posts_dw() {
       wp_add_dashboard_widget( 'wps_recent_posts_dw', __( 'Recent Posts' ), 'wps_recent_posts_dw' );
}
add_action('wp_dashboard_setup', 'add_wps_recent_posts_dw' );

Related posts

2 comments

  1. Replace the_date() with the_time() and it should work fine.

    the_date() only displays the date for posts that are on different days. If two posts are published on the same day, only the first one’s date will be displayed.

  2. As the codex for the_date states:

    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.

    So use the_time or get_the_date instead.

Comments are closed.