0

I am trying to group my posts on the index page by date. For example:
Date 1

Post 1
Post 2
Post 3

Date 2

Post 1
Post 2
Post 3

For some reason, I keep getting the posts under each date. Here is my code, I cant work out where I am going wrong

<?php
$args = array('posts_per_page' => -1, 'orderby' => 'date' );
$myQuery = new WP_Query($args);
$date = '';
if ( $myQuery->have_posts() ) : while ( $myQuery->have_posts() ) : $myQuery- 
>the_post();
if ( $date != get_the_date() ) {
echo $date;
} ?>
<time class="post-date" datetime="<?php the_time('Y-m-d'); ?>"><?php 
the_time('F jS, Y') ?></time>
<?php
$categories = get_the_category();
foreach ( $categories as $category ) {
    echo '<div class="row">
    <div class="column left"><a href="' . esc_url( get_category_link( 
$category ) ) . '">
    <img src="' . esc_url( 'http://example.com/icons/' . intval( $category- 
>term_id ) . '.png' ) . '" alt="' . esc_attr( $category->name ) . '" /></a> 
</div>'; 
}
?>

<div class="column right"><?php the_content(); ?></div>
<?php 
endwhile; endif;
wp_reset_postdata();
?>
  • "I keep getting the posts under each date" Isn't that what you want to do? What's wrong with this? – Jacob Peattie Aug 30 '18 at 13:23
1
<?php 
$day_check = '';
while (have_posts()) : the_post(); 
  $day = get_the_date('j');
  if ($day != $day_check) {
    if ($day_check != '') {
      echo '</ul>'; // close the list here
    }
    echo get_the_date() . '<ul>';
  }
?>
<li><a href="#"><?php the_time(); ?><?php the_title(); ?></a></li>
<?php 
$day_check = $day;
endwhile; ?>

do like this i hop this work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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