WordPress.org

Ready to get started?Download WordPress

Forums

divide loop by days (9 posts)

  1. Mistainu
    Member
    Posted 5 years ago #

    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>
  2. Aries-Belgium
    Member
    Posted 5 years ago #

    <?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; ?>
  3. Mistainu
    Member
    Posted 5 years ago #

    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?

  4. Mistainu
    Member
    Posted 5 years ago #

    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; ?>
  5. Mistainu
    Member
    Posted 5 years ago #

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

  6. Aries-Belgium
    Member
    Posted 5 years ago #

    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;
    }
  7. Mistainu
    Member
    Posted 5 years ago #

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

  8. Hafiz Rahman
    Member
    Posted 5 years ago #

    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

  9. Mistainu
    Member
    Posted 5 years ago #

    @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?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags