年別パラメータを判断してカレント表示させたい
-
年別ごとに見られるように、プルダウンメニューで年別リストを作成しております。
年別を選択すると「/news/?y=2020」といったようにパラメータがつきます。
パラメータの年を判断して、該当の<li>
タグに「class=”selected”」を入れたいのですが、
どのように記述したらよろしいでしょうか。何卒よろしくお願いいたします。
<?php $y = $_GET[ 'y' ]; $m = 1; // 年度の開始月 // 一番古い投稿を取得 $args_oldest_post = array( 'post_type' => 'post', 'posts_per_page' => 1, 'order' => 'ASC', 'orderby' => 'date', ); $oldest_post = new WP_Query( $args_oldest_post ); if ( $oldest_post->have_posts() ): while ( $oldest_post->have_posts() ): $oldest_post->the_post(); // 一番古い投稿の年、月を取得 $oldest_post_y = get_the_date( 'Y' ); $oldest_post_m = get_the_date( 'n' ); endwhile; endif; wp_reset_postdata(); // ナビの開始年を設定 if ( $m > $oldest_post_m ) { $start = $oldest_post_y - 1; } else { $start = $oldest_post_y; } // 現在の年、月を取得 $endYear = date( 'Y' ); $endMonth = date( 'n' ); // ナビの終了年を設定 if ( $m <= $endMonth ) { $end = date( 'Y' ); } else { $end = date( 'Y' ) - 1; } // ナビを出力 echo '<ul class="news__select_list">'; echo '<li class="selected"><a href="/news/">すべて</a></li>'; for ( $i = $end; $i >= $start; $i-- ) { echo '<li><a href="/news/?y=' . $i . '">' . $i . '年</a></li>'; } echo '</ul>'; ?>
3件の返信を表示中 - 1 - 3件目 (全3件中)
3件の返信を表示中 - 1 - 3件目 (全3件中)
- トピック「年別パラメータを判断してカレント表示させたい」には新たに返信することはできません。