【发布时间】:2018-03-11 16:16:26
【问题描述】:
我正在使用 WordPress 自定义帖子类型来管理和显示我网站上的事件。 为了对事件进行排序,我使用了一个包含日期的自定义元字段(存储方式如下:YYYY-MM-DD)。我根据当前日期检查元字段。像这样:
$current_date_query = date ('Y-m-d');
$temp = $wp_query; $wp_query= null; $wp_query = new WP_Query();
$wp_query->query(
array(
'post_type' => 'event',
'meta_key' => 'event_date',
'meta_compare' => '>=',
'meta_value' => $current_date_query,
'post_status' => 'publish',
'posts_per_page' => '99',
'orderby' => 'meta_value',
'order' => 'ASC',
'paged' => $paged
)
); ?>
... Loop stuff ...
<?php endif; $wp_query = null; $wp_query = $temp; wp_reset_query(); ?>
我现在想在每个新月之前添加一个标题。像这样:
2018 年 1 月
- 事件 1
- 事件 2
- 事件 3
2018 年 2 月
- 事件 4
- 事件 5
2018 年 3 月
- 事件 5
等等……
有没有办法在每个月之后拆分循环? 我想我必须使用元字段中的月份,但我不知道如何。
我尝试了以下解决方案(感谢@FluffyKitten 的评论):
<?php
/* declare an array to save the data */
$quarters = array();
foreach($posts as $q) {
$donor_date = get_post_meta($q->ID,'gid_22',true);
$donor_month = date('m',strtotime($donor_date));
/* format the date once - best practice is not to repeat code */
$formatteddate = date('d/m/Y',strtotime($donor_date));
/* organise the dates by quarter, using the heading as the key for easy display */
if(in_array($donor_month, array('01')))
$quarters["January"][] = $formatteddate;
else if(in_array($donor_month, array('02')))
$quarters["February"][] = $formatteddate;
else if(in_array($donor_month, array('03')))
$quarters["March"][] = $formatteddate;
else if(in_array($donor_month, array('04')))
$quarters["April"][] = $formatteddate;
else if(in_array($donor_month, array('05')))
$quarters["May"][] = $formatteddate;
else if(in_array($donor_month, array('06')))
$quarters["June"][] = $formatteddate;
else if(in_array($donor_month, array('07')))
$quarters["July"][] = $formatteddate;
else if(in_array($donor_month, array('08')))
$quarters["August"][] = $formatteddate;
else if(in_array($donor_month, array('09')))
$quarters["September"][] = $formatteddate;
else if(in_array($donor_month, array('10')))
$quarters["Ocotber"][] = $formatteddate;
else if(in_array($donor_month, array('11')))
$quarters["November"][] = $formatteddate;
else if(in_array($donor_month, array('12')))
$quarters["December"][] = $formatteddate;
}
/* now go through your array and display the results */
foreach ($quarters as $quartername => $quarter){
/* $quarters won't have any entry for quarters with no dates, so technically this 'if' isn't needed,
however I've added it in case it will be needed it for any other changes you make */
if ($quarter){ ?>
<h3><?php echo $quartername; ?></h3>
<table>
<?php foreach ($quarter as $qdate ){ ?>
<tr><td> <?php echo $qdate; ?> </td></tr>
<?php } // end foreach($quarter...) ?>
</table>
<?php
} // end if
} // end foreach($quarters...)
?>
现在我得到这样的结果:
十月
- 02/10/2018
- 29/10/2017
一月
- 02/01/2018
十二月
- 2017 年 2 月 12 日
- 2017 年 1 月 12 日
五月
- 02/05/2018
...
订单基于发布日期,而不是基于自定义字段。 年份之间也没有区别。 2017 年应该有它自己的循环,就像 2018 年一样。
如果我尝试将自己的数组放在循环的第一个数组中,它没有任何效果,它只会在前端显示为文本。
编辑:我可以使用这样的自定义查询来订购月份:
$posts = query_posts( array(
'post_type' => 'event',
'meta_key' => 'event_date',
'meta_compare' => '>=',
'meta_value' => $current_date_query,
'post_status' => 'publish',
'posts_per_page' => '99',
'orderby' => 'meta_value',
'order' => 'ASC',
'paged' => $paged
) );
现在的问题只是年份。都是按月排序的,每个月都有不同的年份。
【问题讨论】:
-
谢谢,我试试
-
感谢@FluffyKitten,但我无法让它工作。我已经更新了我的问题中的代码。你有什么想法吗?
标签: php wordpress wordpress-theming