向_Date中的最后一条帖子添加类

时间:2011-03-28 作者:sta777

我正在使用<?php the_date(\'l jS F Y\',\'<h2>\',\'</h2>\'); ?> 在循环中按日期对帖子进行分组/排序。这一切都很好,但我想为每个日期的最后一篇文章添加一个不同的类,有效地分隔每个日期部分。

有人知道怎么做吗?我好像什么都找不到!非常感谢,S。

代码如下:

<?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
根据Bainternet的回答更新了以下代码:

<?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>\'); ?>
然后我回音$hrbtm 就在endwhile:

<?php echo $hrbtm; ?> <?php endwhile; ?>    

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

您可以使用get_adjacent_post 作用

将代码更改为:

<?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" />

结束

相关推荐