0

I'm using <?php the_date('l jS F Y','<h2>','</h2>'); ?> inside the loop in order to group/sort posts by date. This all works great but I would like to add a different class to the last post for each date, effectively seperating each date section.

Does anybody know how to do this please? I can't seem to find anything! Many thanks, S.

Code below:

<?php while ( have_posts() ) : the_post(); ?>
<?php the_date('l jS F Y','<h2>','</h2>'); ?>   
<hr />   
<div class="post">                  
    <h3><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h3>                                     
    <?php the_excerpt(); ?>             
</div> <?php endwhile; ?> <hr class="btm" /> //want this to appear at the end of each date/section

Updated code based on Bainternet's answer below:

<?php while ( have_posts() ) : the_post(); 
            $curent_date = $post->post_date;
            $curent_date = substr($curent_date,0,strpos($curent_date," "));
            $next_post = get_adjacent_post(false,'',false) ;
            if (!$next_post == ''){
            $next_date = $next_post->post_date;
            $next_date = substr($next_date,0,strpos($next_date," "));
            if ($next_date != $curent_date){
            $hrbtm = '<hr class="btm" />';
            echo $hrbtm;
                }
            } else {
            $hrbtm = '';
            }
            ?>      
            <?php the_date('l jS F Y','<h2>','</h2>'); ?>

Then I echo $hrbtm just before the endwhile:

<?php echo $hrbtm; ?> <?php endwhile; ?>    
3
  • 1
    Please show your loop/code.
    – kaiser
    Commented Mar 28, 2011 at 14:00
  • Maybe this will help? wordpress.stackexchange.com/questions/6346/… One note: it'll be a lot easier for you to style the first post in each than the last one. Can you work with that? Commented Mar 28, 2011 at 15:27
  • Thanks goldenapples - I did see that post before, but I had already got that stage working with the <?php the_date(); ?>. I'm afraid it is definetly the last post that I need to add the class to : ( Is it even possible?
    – sta777
    Commented Mar 28, 2011 at 15:47

1 Answer 1

0

Its possible, you can get the next post date to compare using get_adjacent_post function

change your code to this:

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

//hold current date
$curent_date = $post->post_date;
//fix the format to YYYY-MM-DD
$curent_date = substr($curent_date,0,strpos($curent_date," "));
$next_post = get_adjacent_post(false,'',false) ;
if (!$next_post == ''){
    //get next post's date
    $next_date = $next_post->post_date;
    //fix the format to YYYY-MM-DD
    $next_date = substr($next_date,0,strpos($next_date," "));
    if ($next_date != $curent_date){//last post of the date
        the_date('l jS F Y','<h2 class="lats-of-date>','</h2>');
    }
}else{
 the_date('l jS F Y','<h2>','</h2>'); 
}
?>
<hr />   
<div class="post">                  
    <h3><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h3>                                     
    <?php the_excerpt(); ?>             
</div> <?php endwhile; ?> <hr class="btm" />
2
  • Many thanks for the code - I did have to amend it slightly to do what I required, but it now works perfectly! Thank you. (Updated code added to original question)
    – sta777
    Commented Mar 29, 2011 at 8:32
  • 1
    @ss888 Then mark this question as answered.
    – fuxia
    Commented Mar 29, 2011 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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