我有一个由WP_Query->帖子
返回的对象数组array
0 =>
object(WP_Post)
public 'ID'
public 'post_title'
public 'post_content'
1 =>
object(WP_Post)
public 'ID'
public 'post_title'
public 'post_content'
2 =>
object(WP_Post)
public 'ID'
public 'post_title'
public 'post_content'
3 =>
object(WP_Post)
public 'ID'
public 'post_title'
public 'post_content'
4 =>
object
public 'Donor'
public 'date'
public 'post_content'
它继续......我试图根据date
元字段将其分为第一,第二,第三和第四季度,即;第一季度是前三个月(1月,2月,3月),第二季度将是接下来的三个月等,并给出标题,第一季度,第二季度'等: -
我是通过
实现的<h3> First Quarter </h3>
<?php
foreach($posts as $q1) {
$donor_date = get_post_meta($q1->ID,'donor-date',true);
$donor_month = date('m',strtotime($donor_date));
if(!in_array($donor_month,array('01','02','03'),true)) continue;
?>
<tr><td> <?php echo date('d/m/Y',strtotime($donor_date)); ?> </td></tr>
<?php endforeach; ?>
<h3> Second Quarter </h3>
<?php
foreach($posts as $q2) {
$donor_date = get_post_meta($q2->ID,'donor-date',true);
$donor_month = date('m',strtotime($donor_date));
if(!in_array($donor_month,array('04','05','06'),true)) continue;
?>
<tr><td> <?php echo date('d/m/Y',strtotime($donor_date)); ?> </td></tr>
<?php endforeach; ?>
等等......这种方法的问题在于,即使没有符合给定条件的数据,标题(第一季度,第二季度......)也会存在。有没有办法有条件地显示和隐藏标题和更好的排序方法(重构).......
提前致谢........
答案 0 :(得分:1)
在开始任何输出之前,您需要检查数组中的值。
我建议通过保存要在数组中显示的数据来代替
立即打印。另外,假设每个$posts
中foreach
相同,您只需要循环一次。
评论中的分步说明示例
<?php
/* declare an array to save the data */
$quarters = array();
foreach($posts as $q) {
$donor_date = get_post_meta($q->ID,'donor-date',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','02','03')))
$quarters["First Quarter"][] = $formatteddate;
else if(in_array($donor_month, array('04','05','06')))
$quarters["Second Quarter"][] = $formatteddate;
else if(in_array($donor_month, array('07','08','09')))
$quarters["Third Quarter"][] = $formatteddate;
else if(in_array($donor_month, array('10','11','12')))
$quarters["Fourth Quarter"][] = $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...)
?>
我不确定您的<table>
元素的创建位置,因为目前在<h3>
之间添加了<tr>
,这是不正确的 - 但这会反映您发布的代码,所以你应该知道如何解决这个问题!
答案 1 :(得分:0)
将标题放在for循环中并使用if和else if语句验证月份,以选择哪个标题显示使用一个foreach语句
<?php
foreach($posts as $q) {
$donor_date = get_post_meta($q1->ID,'donor-date',true);
$donor_month = date('m',strtotime($donor_date));
if(in_array($donor_month,array('01','02','03'),true))
echo "<h3>First Quarter</h3>";
if(!in_array($donor_month,array('04','05','06'),true)
echo "<h3>Second Quarter</h3>";
?>
<tr><td> <?php echo date('d/m/Y',strtotime($donor_date)); ?> </td></tr>
<?php endforeach; ?>