是否按月+年的格式对发布的事件进行分组?

时间:2019-11-12 作者:soofz

如何以以下格式打印即将举行的活动。

2019年6月

特拉拉拉瓦拉

2019年5月

特拉拉拉瓦拉

我的args数组是

 $arg = array(
                \'post_type\' => \'events-promotion\',
                \'posts_per_page\' => -1,
                \'post_status\' => \'publish\',
                \'meta_key\'=>\'wpcf-event-date\',
                \'meta_value\' => $curdate,
                \'meta_compare\' => \'>=\',
                \'orderby\'=>\'meta_value\',
                \'order\' => \'DESC\',
    );

1 个回复
SO网友:soofz

$arg = array(
            \'post_type\' => \'events-promotion\',
            \'posts_per_page\' => -1,
            \'post_status\' => \'publish\',
            \'meta_key\'=>\'wpcf-event-date\',
            \'meta_value\' => $curdate,
            \'meta_compare\' => \'>=\',
            \'orderby\'=>\'meta_value\',
            \'order\' => \'DESC\',
);

$the_query2 = new WP_Query( $arg );
$arr=array();
if ( $the_query2->have_posts() ) :
while ( $the_query2->have_posts() ) : $the_query2->the_post();
$eveid2 = get_the_ID();
$evedate2 = get_post_meta($eveid2,\'wpcf-event-date\',true);
$evedt = date(\'d/m/Y\', $evedate2);
$pdate = date_i18n( \'M j, Y\', $evedate2 );
$year=date(\'Y\', $evedate2);
$month=date(\'m\', $evedate2);

$arr[$year][$month][]=get_the_ID();
   endwhile;
endif; 

foreach($arr as $index => $ar1){


    foreach($ar1 as $index =>$ar2){

        ?>
        <div class="col-sm-12">

        <?php

          $monthName = date(\'F\', mktime(0, 0, 0, $index, 10)); // March
          echo \'<h4>\'.$monthName.\'</h4>\';
        foreach($ar2 as $ar3){

    $evelink = get_post_meta($ar3,\'wpcf-event-link\',true);
$evetitle = get_the_title($ar3);
$image = wp_get_attachment_image_src( get_post_thumbnail_id($ar3), \'single-post-thumbnail\' );
 $evedate2 = get_post_meta($ar3,\'wpcf-event-date\',true);
$evedt = date(\'d/m/Y\', $evedate2);
    ?>
    <div class="col-sm-4 plcholder">
  <div class="imgpart"><img src="<?php echo $image[0]?>"></div>
  <div class="evepart">
      <p><?php echo $evedt;?></p>
      <h6><?php echo $evetitle;?></h6>
  </div>
  </div>
    <?php
    }
    ?></div><?php
    }
}
?>