我有一个自定义类别模板,可以根据自定义字段“event\\u date”对帖子进行分组。有两件事我无法正确理解:按年份和月份分组,按降序排列,而帖子本身则按升序排列。目前,这将为每个帖子创建“事件年”div和“事件月”。我想我在每个“年”和“月”都需要一些for/each语句但是,我只是没有经验把它写对。
因此,最终的标记是:
<div event-year>
<div event-month>
<article>
<article>
</div>
<div event-month>
<article>
</div>
<div event-month>
</div>
<div event-year>
<div event-month>
<article>
<article>
</div>
</div>
这就是我现在拥有的:
<?
$the_query = new WP_Query( array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'meta_key\' => \'event_date\',
\'orderby\' => \'meta_value\'
) );
$current_header = \'\';
$current_year = \'\';
$current_month = \'\';
$current_day = \'\';
# The Loop
while ( $the_query->have_posts() ) :
$the_query->the_post();
# get the event date
$temp_date = get_post_meta( get_the_ID(), \'event_date\', true );
Break up the date format
$dateTime = DateTime::createFromFormat("Ymd", $temp_date);
if ( is_object($dateTime) ) {
$month = $dateTime->format(\'F\');
$year = $dateTime->format(\'Y\');
$day = $dateTime->format(\'d\');
//...
}
$display_month = substr($month, 0, 3);
if($year != $current_year) {
$current_year = $year;
echo "<div id=\'event-year\' data-year-group=\'$year\'>";
}
if($month != $current_month) {
$current_month = $month;
echo "<div class=\'event-month\'><h2>$year - $month</h2>";
}
?>
<article> post stuff in here... </article>
<? endwhile; ?>