Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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; ?>    
share|improve this question
1  
Please show your loop/code. –  kaiser Mar 28 '11 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? –  goldenapples Mar 28 '11 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 Mar 28 '11 at 15:47

1 Answer 1

up vote 0 down vote accepted

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" />
share|improve this answer
    
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 Mar 29 '11 at 8:32
1  
@ss888 Then mark this question as answered. –  toscho Mar 29 '11 at 9:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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