我有一个帖子类型,其中包含一个带有日期的自定义元字段。日期以以下格式存储:yyyymmdd
.
在这个帖子类型的归档页面上,我想创建12个单独的容器,每个月一个,并在每个帖子中显示与相应月份匹配的内容。
我的问题是寻找实现我想要的想法。
多亏了Krzysiek Dródżidea,我过滤了pre\\u get\\u帖子,并在我的归档模板中为帖子类型生成了以下代码:
$posts_by_month = array()
while ( have_posts() ) : the_post();
// get the post meta where the date is stored in yyyymmdd format
$recurrence_date = strtotime( get_post_meta( $post->ID,\'recurrence_futuredates_0_recurrence_from\', true ) );
// return the month only in \'1-12\' format
$current_month = date( \'n\', $recurrence_date );
// loop 12 months
for ( $i=1; $i<=12; $i++ ) {
if ( $current_month == $i ) {
// store all the posts for the $current_month
$posts_by_month[$current_month][] = array(
\'posts\' => array(
\'id\' => $post->ID,
\'title\' => get_the_title(),
\'permalink\' => get_permalink(),
),
);
}
}
endwhile;
// sort the month order
ksort( $posts_by_month );
// loop 12 months
for ( $i=1; $i<=12; $i++ ) {
// if there are no posts for current month, skip month
if ( $posts_by_month[$i] ) {
?>
<i><?php echo $i; ?></i>
<?php foreach ( $posts_by_month[$i] as $this_month[$i] ) : ?>
<b><?php echo $this_month[$i][\'title\']; ?></b><br/>
<?php endforeach;
}
}