WordPress.org

Support

Support » Plugins and Hacks » divide loop by days

divide loop by days

  • how do you write a loop that divides the posts into groups by days?
    some kind of query?
    if a theme already does this, please provide theme name and/or link
    example

    <h3>January 3, 2008</h3>
    <div>first post here</div>
    <div>second post here</div>
    
    <h3>January 4, 2008</h3>
    <div>third post here</div>
    
    <h3>January 6, 2008</h3>
    <div>another post here</div>
Viewing 8 replies - 1 through 8 (of 8 total)
  • Aries-Belgium
    Member

    @aries-belgium

    <?php
    $prev_date = null;
    $posts = get_posts(array('orderby'=>'post_date'));
    foreach( $posts as $post ):
    $date = date( "F j, Y" , strtotime( $post->post_date ) );
    if( $date != $prev_date ): ?>
    <h3><?php echo $date ?></h3>
    <?php
    $prev_date = $date;
     endif; ?>
    <div><?php echo $post->post_content ?></div>
    <?php endforeach; ?>

    how is this done with a regular loop? i did this:

    <?php /* Start the loop */ if ( have_posts() ) : while ( have_posts() ): the_post(); ?>
    
    <?php
    $prev_date = null;
    $posts = get_posts(array('orderby'=>'post_date'));
    foreach( $posts as $post ):
    $date = date( "F j, Y" , strtotime( $post->post_date ) );
    if( $date != $prev_date ): ?>
    
    <h3><?php echo $date ?></h3>
    <?php $prev_date = $date; endif; ?>
    
    <div class="<?php if ( is_home() or is_front_page() ) { ?>hentry<?php } ?>" id="post-<?php the_ID(); ?>">
    		<div class="entry-head">
    			<h3 class="entry-title">
    				<a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    			</h3>
    			<?php if ( comments_open() ) : comments_popup_link( '<span>0</span>', '<span>1</span>', '<span>%</span>', 'commentslink', ''); endif; ?>
    <span class="entry-date">Published <abbr class="published" title="<?php the_time('Y-m-d\TH:i:sO'); ?>"><?php if (function_exists('time_since')) {
    echo time_since(abs(strtotime($post->post_date_gmt . " GMT")), time()) . " ago";
    } else {
    the_time('H:i:s a'); ?> on <?php the_time('F j, Y');
    } ?></abbr></span>
    		</div>
    		<div class="entry-meta">
    			<span class="entry-author">
    				<address class="vcard author">
    					<span class="fn"><?php the_author(); ?></a>
    				</address>
    			</span>
    			<span class="entry-categories"><?php the_category(', '); ?></span>
    		</div>
    		<div class="entry-content"><?php the_content(); ?></div>
    </div>
    
    <?php endforeach; ?>
    
    <?php endwhile; /* End The Loop */ else:
    	include("fourohfour.php");
    endif; ?>

    but the loop repeats 3 times? what happened?

    a bit off-topic, but how about changing it to “today” and “yesterday” if the posts below were actually today or yesterday?

    nevermind, fixed it myself, just a matter of rearranging the terms.

    <?php /* Start the loop */ if ( have_posts() ) : while ( have_posts() ): ?>
    
    <?php
    $prev_date = null;
    $posts = get_posts(array('orderby'=>'post_date'));
    foreach( $posts as $post ):
    $date = date( "F j, Y" , strtotime( $post->post_date ) );
    if( $date != $prev_date ): ?>
    
    <h3><?php echo $date ?></h3>
    <?php $prev_date = $date; endif; ?>
    
    <?php the_post(); ?>
    
    <div class="<?php if ( is_home() or is_front_page() ) { ?>hentry<?php } ?>" id="post-<?php the_ID(); ?>">
    		<div class="entry-head">
    			<h3 class="entry-title">
    				<a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    			</h3>
    			<?php if ( comments_open() ) : comments_popup_link( '<span>0</span>', '<span>1</span>', '<span>%</span>', 'commentslink', ''); endif; ?>
    <span class="entry-date">Published <abbr class="published" title="<?php the_time('Y-m-d\TH:i:sO'); ?>"><?php if (function_exists('time_since')) {
    echo time_since(abs(strtotime($post->post_date_gmt . " GMT")), time()) . " ago";
    } else {
    the_time('H:i:s a'); ?> on <?php the_time('F j, Y');
    } ?></abbr></span>
    		</div>
    		<div class="entry-meta">
    			<span class="entry-author">
    				<address class="vcard author">
    					<span class="fn"><?php the_author(); ?></a>
    				</address>
    			</span>
    			<span class="entry-categories"><?php the_category(', '); ?></span>
    		</div>
    		<div class="entry-content"><?php the_content(); ?></div>
    </div>
    
    <?php endforeach; ?>
    
    <?php endwhile; /* End The Loop */ else:
    	include("fourohfour.php");
    endif; ?>

    i still want to know how to change the date to “today” and “yesterday”.

    Aries-Belgium
    Member

    @aries-belgium

    You can delete the while loop because it doens’t do anything anymore.

    i still want to know how to change the date to “today” and “yesterday”.

    switch(true)
    {
          case $post->post_date >= strtotime("today"):
              $datestr = "Today";
          break;
          case $post->post_date >= strtotime("yesterday"):
              $datestr = "Yesterday";
          break;
          default:
              $datestr = date( "F j, Y" , strtotime( $post->post_date ) );
    ;
          break;
    }
    Mistainu
    Member

    @mistainu

    where does that go? could you rewrite the loop with that in it?

    Hi,

    the function the_date() can actually be used for this feature you want without adding extra codes inside the loop.

    http://codex.wordpress.org/Template_Tags/the_date

    Mistainu
    Member

    @mistainu

    @hafiz i’ll use that if there was a way to change the date to “today” if it was true and “yesterday” also. could you show the full loop with those?

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘divide loop by days’ is closed to new replies.