web関連

【wordpress】archive.phpで一覧周りを一括で管理したい時の書き方

【wordpress】archive.phpで一覧周りを一括で管理したい時の書き方

archive.php1つだけで年月の一覧やターム一覧まとめたかった時の備忘録

archive.phpで一覧周りを管理する

<?php
// ページ数出力?する用
$paged = (int) get_query_var('paged');
// 年月別アーカイブ用
$year = get_query_var('year');
$monthnum = get_query_var('monthnum');
// 投稿とカスタム投稿のスラッグを取得
$post_type = esc_html(get_post_type_object(get_post_type())->name);
$args = array(
    'year' => $year,
    'monthnum' => $monthnum,
    'paged' => get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1,
    'post_type' => $post_type,
    'posts_per_page' => $list,
);
if(is_tax()){ //ターム一覧ページ
  //タクソノミーのスラッグ取得
  $taxonomy_slug = get_query_var('taxonomy');
  $term = $wp_query->get_queried_object();
  $args = array(
    'tax_query' => 
      array(
        array(
          'taxonomy' => $taxonomy_slug,
          'terms' => array( $term->slug ),
          'field'=>'slug',
        ),
      ),
  );
}
if(is_category()){ //投稿のカテゴリー一覧
  $cat = get_queried_object();
  $args = array(
    'cat' => $cat->term_id,
  );
}
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) :
  while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<!-- 本文が入ります -->
<?php the_title(); ?>
<!-- 本文が入ります -->
<?php endwhile; ?>
<?php else: ?>
<?php endif; ?>

間違えてたらごめん
とりあえずのメモ

Leave a Comment

入力エリアすべてが必須項目です。メールアドレスが公開されることはありません。

内容をご確認の上、送信してください。