舞台公演の情報を表示するページを作成しております。
カスタムフィールドで入力した内容を取得して一覧表示させるコードを作成し、表示させています。
現状は、入力した日付(yy/mm/dd)を
'value' => array('2018/04/01','2018/04/31'),
のように、期間を指定して、任意の期間の情報を表示させています。
これを「今月」「先月」「来月」という3種類の扱いで、動的に期間を算出して表示させるようにしたいと考えているのですが、どのようなコードとすれば実現できるでしょうか。
初歩的な質問で恐縮ですが、ご教示いただけますと幸いです。
現状コードは以下です。
<?php $posts = get_posts( array( 'numberposts' => -1, 'post_type' => 'kouen', 'orderby' => 'meta_value', 'meta_key' => 'schedule_start', 'order' => 'ASC', 'meta_query' => array( 'key' => 'schedule_start', 'value' => array('2018/04/01','2018/04/31'), 'compare' => 'BETWEEN', 'type'=>'DATE' ) ) ); global $post; ?> <?php if($posts): foreach($posts as $post): setup_postdata($post); ?> <div style="padding:15px 0;border-bottom:1px dotted #dddddd;line-height:140%;"> <span style="font-size:90%;"> <?php $week = array("日", "月", "火", "水", "木", "金", "土"); ?> <?php $date = date_create(''.get_field('schedule_start').''); echo date_format($date,'n月j日') . "(" . $week[(int)date_format($date,'w')] . ")" ; ?> 〜 <?php $date = date_create(''.get_field('schedule_end').''); echo date_format($date,'n月j日') . "(" . $week[(int)date_format($date,'w')] . ")" ; ?> </span> <br> <span style="margin-top:5px;margin-bottom:5px;display:block;"> <strong> <?php the_field('team_name'); ?>『<?php the_field('title'); ?>』 </strong> <span style="font-size:80%;"><?php the_field('location'); ?></span> </span> <!--URlがあれば表示、の分岐 ここから--> <?php $imgid = get_field('url'); ?> <?php if(empty($imgid)):?> <?php else:?> <span style="font-size:80%;margin-bottom:10px;display:block;">→ <a href="<?php the_field('url'); ?>" target="_blank"><?php the_field('url'); ?></a></span> <?php endif;?> <!--URlがあれば表示、の分岐 ここまで--> <!--感想1があれば表示、の分岐 ここから--> <?php $imgid = get_field('kanso1_title'); ?> <?php if(empty($imgid)):?> <?php else:?> <!--<span style="font-size:90%;margin-top:10px;margin-bottom:5px;display:block;">観劇レポート</span>--> <span style="font-size:100%;font-weight:bold;">・<a href="<?php the_field('kanso1_url'); ?>" target="_blank"><?php the_field('kanso1_title'); ?></a></span><span style="font-size:90%;"> by<?php the_field('kanso1_user'); ?></span><br> <?php endif;?> <!--感想1があれば表示、の分岐 ここまで--> <!--感想2があれば表示、の分岐 ここから--> <?php $imgid = get_field('kanso2_title'); ?> <?php if(empty($imgid)):?> <?php else:?> <span style="font-size:100%;font-weight:bold;">・<a href="<?php the_field('kanso2_url'); ?>" target="_blank"><?php the_field('kanso2_title'); ?></a></span><span style="font-size:90%;"> by<?php the_field('kanso2_user'); ?></span><br> <?php endif;?> <!--感想2があれば表示、の分岐 ここまで--> <!--感想3があれば表示、の分岐 ここから--> <?php $imgid = get_field('kanso3_title'); ?> <?php if(empty($imgid)):?> <?php else:?> <span style="font-size:100%;font-weight:bold;">・<a href="<?php the_field('kanso3_url'); ?>" target="_blank"><?php the_field('kanso3_title'); ?></a></span><span style="font-size:90%;"> by<?php the_field('kanso3_user'); ?></span><br> <?php endif;?> <!--感想3があれば表示、の分岐 ここまで--> <!--感想4があれば表示、の分岐 ここから--> <?php $imgid = get_field('kanso4_title'); ?> <?php if(empty($imgid)):?> <?php else:?> <span style="font-size:100%;font-weight:bold;">・<a href="<?php the_field('kanso4_url'); ?>" target="_blank"><?php the_field('kanso4_title'); ?></a></span><span style="font-size:90%;"> by<?php the_field('kanso4_user'); ?></span><br> <?php endif;?> <!--感想4があれば表示、の分岐 ここまで--> <!--感想5があれば表示、の分岐 ここから--> <?php $imgid = get_field('kanso5_title'); ?> <?php if(empty($imgid)):?> <?php else:?> <span style="font-size:100%;font-weight:bold;">・<a href="<?php the_field('kanso5_url'); ?>" target="_blank"><?php the_field('kanso5_title'); ?></a></span><span style="font-size:90%;"> by<?php the_field('kanso5_user'); ?></span><br> <?php endif;?> <!--感想5があれば表示、の分岐 ここまで--> </div><!--data_box--> <?php endforeach; endif;?> <!--公演情報呼び出しここまで--> <!--ひとつき分終わり-->