有条件地使用不同的标题对WP_Query-> posts对象数组进行排序 [英] Sort WP_Query->posts Array of Objects conditionally with Different Headers

查看:48
本文介绍了有条件地使用不同的标题对WP_Query-> posts对象数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有WP_Query-> posts

I am having an array of Objects returned by WP_Query->posts

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 元字段将其分为第一,第二,第三和第四季度,即;第一季度是前三个月(一月,二月,三月),第二季度是接下来的三个月,依此类推,标题为第一季度",第二季度"等:-

And it goes on ...... I am trying to group this into first, second, third and fourth Quarter based on a date meta field ie; first quarter is the first three months (Jan, Feb ,March ), second quarter will be the next three and so on and give headings ,'First Quarter ,'Second Quarter ' etc:-

我是通过

<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; ?>

等等...这种方法的问题在于,即使没有满足给定条件的数据,标题(第一季度,第二季度...)也将存在.有没有办法有条件地显示和隐藏标题,还有更好的排序方法(重构).......

and so on ...... the problem with this approach is that the headings ( First Quarter, Second Quarter ... ) will be there even if there is not data that meets the given condition . Is there a way to conditionally show and hide the headers too and a better method for sorting ( refactor) .......

提前谢谢........

Thanks in advance ........

推荐答案

在开始任何输出之前,您需要检查数组中的值.

You'll need to check the values in the array before starting any output.

我建议通过保存要显示在数组中而不是数组中的数据来做到这一点立即打印.同样,假设每个 foreach 中的 $ posts 是相同的,则只需要循环一次即可.

I'd suggest doing this by saving the data to be displayed in an array instead of printing it immediately. Also, assuming $posts is the same in every foreach, you will only need to loop once.

示例以及注释中的分步说明

<?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> 元素的创建位置,因为在<tr> s这是不正确的-但这会反映您发布的代码,因此您应该自己知道如何解决该问题!

I'm not sure where your <table> element is created,because at the moment the <h3>s are added between <tr>s which is incorrect - however this mirrors the code you posted, so you should know how to work around that yourself!

这篇关于有条件地使用不同的标题对WP_Query-> posts对象数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆