我试图通过如下显示的日期自定义字段显示邮政订单:
Year
Month
Custom field post info(date, time, venue, and category)
Title
我在论坛中找到了这个功能,并对其进行了一些修改,可以很好地按年份、月份和日期进行分类和显示,但现在我已经整理好了帖子,我想添加每篇文章的标题和信息的其余部分,所以我需要的是一种在foreach statemets中显示这些信息的方法,或者是一个有序帖子ID的数组(我认为这个选项对我来说更容易)
<?php
$posts = get_posts(array(
\'post_type\' => \'post\',
\'meta_key\' => \'fecha\',
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\',
\'meta_type\' => \'DATE\'
));
$group_posts = array();
if( $posts ) {
foreach( $posts as $post ) {
$date = CFS()->get( \'fecha\' );
$date = new DateTime($date);
$year = $date->format(\'Y\');
$month = $date->format(\'F\');
$group_posts[$year][$month][] = array($post, $date);
}
}
foreach ($group_posts as $yearKey => $years) {
echo $yearKey;
echo \'<br>\';
foreach ($years as $monthKey => $months) {
echo $monthKey;
echo \'<br>\';
foreach ($months as $postKey => $posts) {
echo $posts[1]->format(\'d-m-Y\');
echo \'<br>\';
echo $posts[0]->title;
echo \'<br>\';
}
}
}
?>
任何帮助都将不胜感激!
SO网友:CarlaG
后来我就知道了。最后使用了不同的方法,我会发布它,以防有人有相同的问题
<?php
date_default_timezone_set(\'America/Argentina/Buenos_Aires\');
$posts_by_date_field = new WP_Query(array(
\'post_type\' => \'post\',
\'posts_per_page\' => -1,
\'meta_key\' => \'fecha\',
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\',
\'meta_type\' => \'DATE\'
));
if ($posts_by_date_field->have_posts()) : while ($posts_by_date_field->have_posts()) : $posts_by_date_field->the_post();
// LOOP
$today = date("Y-m-d");
$date = CFS()->get(\'fecha\');
if ($date >= $today) :
$date = new DateTime($date);
$year = $date->format(\'Y\');
$month = $date->format(\'F\');
if ($aux_year != $year) {
?>
<div class="anio"> <?php echo $year; ?> </div>
<?php
}
if ($aux_month != $month) {
?>
<div class="mes"> <?php echo $mes ?> </div>
<?php
}
?>
<div class="metaData">
<p> <?php the_title(); ?></p>
<p> <?php echo CFS()->get(\'fecha\'); ?> </p>
<p> <?php echo CFS()->get(\'hora\'); ?> </p>
<p> <?php echo CFS()->get(\'lugar\'); ?> </p>
</div>
<?php
$aux_year = $year;
$aux_month = $month;
endif;
endwhile;
endif;
?>