Ask your WordPress questions! Pay money and get answers fast! (more info)

Posts with same date only echo the date once WordPress

  • SOLVED

I wrote a 'latest news' widget for the sidebar to display the 5 most recent posts from a category. I'm echoing the title/permalink, and date to the browser.

The problem I'm having however, is that posts that were written on the same date only display that date once (under the first post), meaning the second post that has the same post date as the post above it, will not display the post date for it.


function widget($args, $instance) {
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
$cat_id = $instance["cat_id"];
?>

<?php if (have_posts()) : query_posts('showposts=5&cat=' . $instance['cat_id']); ?>

<div id="news" class="widget widget_news">
<h3 class="widgettitle"><?php echo $instance['title']; ?></h3>
<?php while (have_posts()) : the_post(); ?>
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<p><?php the_date(); ?></p>
<?php endwhile; ?>
</div><!--/news-->
<?php endif; ?>
<?php wp_reset_query(); ?>
<?php
}

Answers (2)

2010-04-23

Milan Petrovic answers:

Replace this line:

<p><?php the_date(); ?></p>

with this:

<p><?php global $previousday; $previousday = ""; the_date(); ?></p>

2010-04-23

Jarret Minkler answers:

Just move the_date line outside of the loop

function widget($args, $instance) {

extract( $args );

$title = apply_filters('widget_title', $instance['title']);

$cat_id = $instance["cat_id"];

?>



<?php if (have_posts()) : query_posts('showposts=5&cat=' . $instance['cat_id']); ?>



<div id="news" class="widget widget_news">

<h3 class="widgettitle"><?php echo $instance['title']; ?></h3>
<p><?php the_date(); ?></p>

<?php while (have_posts()) : the_post(); ?>

<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>



<?php endwhile; ?>

</div><!--/news-->

<?php endif; ?>

<?php wp_reset_query(); ?>

<?php

}