#最初に
閲覧ありがとうございます。
webサイトをの投稿ページをカスタム投稿で作成しておりまして、その出力方法について質問させてください。
現状のループ文は以下になります
#現状
PHP
<h3></h3> <div> <?php $month = date("m"); $day = date("d"); $custom_posts = get_posts(array( 'post_type' => 'event', 'posts_per_page' => 6, 'orderby' => array( 'event_month' => 'ASC', 'event_day' => 'ASC', ), 'meta_query' => array( 'relation' => 'AND', array( 'event_month' => array( 'key' => 'event_month', 'type' => 'DECIMAL(2,0)', 'compare' => '>=', ), ), array( 'event_day' => array( 'key' => 'event_day', 'type' => 'DECIMAL(2,0)', 'compare' => '>=', ), ), array( 'event_week' => array( 'key' => 'event_week', ), ), array( 'relation' => 'AND', array( 'key' => 'event_month', 'value' => $month, 'type' => 'DECIMAL(2,0)', 'compare' => '>=' ), array( 'key' => 'event_day', 'value' => $day, 'type' => 'DECIMAL(2,0)', 'compare' => '>=' ), ) ), 'tax_query' => array( array( 'taxonomy' => 'event-tag', 'field' => 'slug', 'terms' => array('accepting'), 'operator' => 'IN' ), ) )); global $post; if ($custom_posts) : foreach ($custom_posts as $post) : setup_postdata($post); ?> <div class="block"> <?php $search = array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"); $replace = array("日", "月", "火", "水", "木", "金", "土"); $week = get_post_meta($post->ID, 'event_week', true); $weekJP = str_replace($search, $replace, $week); $weekClass = str_replace($search, $replace, $week); if ($week == 'Sun') : $weekClass = 'sun'; elseif ($week == 'Sat') : $weekClass = 'sat'; else : $weekClass = 'others'; endif; ?> //ここから日付の取得 <span class="month"><?php echo get_post_meta($post->ID, 'event_month', true); ?>/</span> <span class="<?php echo $weekClass ?>"> <span class="day"><?php echo get_post_meta($post->ID, 'event_day', true); ?></span> (<?php echo $weekJP; ?>) </span> //ここまで <a href="<?php the_permalink(); ?>"> <?php setup_postdata($post); echo the_post_thumbnail(array(200, 250)); ?> <p class="title"><?php the_title(); ?></p> </a> </div> <?php endforeach; wp_reset_postdata(); endif; ?> </div>
出力内容は日付とサムネイル、タイトルのみで、以下のようになっています
HTML5
<h3></h3> <div> <div class="block"> <span class="month">12/23(金)</span>//省略して表記しています <a href="#"><img><p class="title">タイトル</p></a> </div> <div class="block"> <span class="month">12/23(金)</span> <a href="#"><img><p class="title">タイトル</p></a> </div> <div class="block"> <span class="month">12/24(土)</span> <a href="#"><img><p class="title">タイトル</p></a> </div> <div class="block"> <span class="month">12/24(土)</span> <a href="#"><img><p class="title">タイトル</p></a> </div>
#実現したいこと
同じ日付が連続で並ぶのは正直見栄えが良くないので、できれば下記のように
日付ごとにブロック化して出力したいと考えております。
HTML5
<h3>12/23(金)</h3> <div> <div class="block"> <a href="#"><img><p class="title">タイトル</p></a> </div> <div class="block"> <a href="#"><img><p class="title">タイトル</p></a> </div> <h3>12/24(土)</h3> <div class="block"> <a href="#"><img><p class="title">タイトル</p></a> </div> <div class="block"> <a href="#"><img><p class="title">タイトル</p></a> </div>
#試したこと
ループを使って出力を制御すれば良いのかな...と思いましたが、あまりうまくいきません。
どなたかご教授いただける方、よろしくお願いいたします。
まだ回答がついていません
会員登録して回答してみよう