WP 固定ページにカスタム投稿一覧を作成

1.固定ページを作成

ダッシュボードから固定ページを作成して、URLスラッグ名を設定 (例:staff)

2.専用のページテンプレートを作成

page-◯◯.phpを作成 (◯◯はURLスラッグ名 例:page-staff.php

page-◯◯.php( WP_Query( $args ) でカスタム投稿slagを指定)

<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<?php get_header(); ?>

<?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>


    <?php
         $paged = get_query_var('paged') ?: 1; //ページネーションを使いたいなら指定
         $args = array(
          'paged' => $paged, //ページネーションを使いたいなら指定
          'posts_per_page' => 3, //3記事のみ出力
          'post_status' => 'publish', //公開の記事だけ
          'post_type' => 'staffs', //カスタム投稿slag
          'orderby' => 'date', //日付を出力する基準
          'order' => 'DESC' //表示する順番(逆はASC)

         );

         $the_query = new WP_Query( $args );
         if ( $the_query->have_posts() ) :
         ?>

         <?php 
           while ( $the_query->have_posts() ) : $the_query->the_post();
         //-------- ここから繰り返し---------- 
         ?>

          <dl class='staff'>
            <dt class='staff-title'>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?>
                </a>
                <!--slug取得-->
                <?php $name_en = strtoupper($post->post_name); ?>
                <p><?= str_replace('-', ' ', $name_en); ?></p>          
            </dt>
            <dd class="staff-pic">
                <a href="<?php the_permalink(); ?>">
                    <?php if ( has_post_thumbnail() ): ?>
                        <?php the_post_thumbnail('medium'); ?>
                    <?php else: ?>
                        <img src="<?php echo get_template_directory_uri(); ?>/assets/img/common/noimage_600x400.png" alt="">
                    <?php endif; ?>
                </a>
            </dd>
            <dd class='staff-date'>
              <!--the_date()だと同じ日付が非表示になるので注意-->
              <?php the_time('Y年m月d日'); ?>
            </dd>
            <dd class='staff-cat'>
                <!--※タクソノミー設定後:ハイパーリンク有り-->
                <!--get_the_term_list( $id, $taxonomy, '前に挿入する文字列', '区切り文字', '後に挿入する文字列' )-->
                <?php echo get_the_term_list($post->ID, 'job_cat', '', '|',''); ?>
              </dd>
            <dd class='staff-cat'>
                <!--※タクソノミー設定後:ハイパーリンク無し-->
                <?php
                if ($terms = get_the_terms($post->ID, 'job_cat')) {
                    foreach ( $terms as $term ) {
                        echo esc_html($term->name);

                        //区切り文字
                        if( next($terms) ){
                            echo '|';
                        }
                    }
                }
                ?>
            </dd>
            <dd class='staff-content'><?php the_content(); ?></dd>
          </dl>

         <?php //-------- 繰り返しここまで-----------
         endwhile; ?>

         <?php //-------- WP_query終了-----------
                     wp_reset_postdata();      
         endif; ?>


    <?php endwhile; ?>
<?php endif; ?>

<?php get_footer(); ?>

カスタム投稿のカスタム分類のタームごとの一覧を表示する場合

new WP_Query( $args )で'tax_query'を指定する

 $args = array(
  'post_type' => 'staffs', //カスタム投稿slag
  'orderby' => 'date', //日付を出力する基準
  'order' => 'DESC', //表示する順番(逆はASC)
  'tax_query' => array(//カスタム分類 job_ca の sales に関連付けられた投稿(post)を表示
    array(
      'taxonomy' => 'job_cat',
      'field'    => 'slug',
      'terms'    => 'sales',
    ),
  ),
 );

 $the_query = new WP_Query( $args );

全体だと下記のようになる

※繰り返し部分はパーツにして読み込み(template-partsフォルダにloop-staff.phpを作成したとする)

<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<?php get_header(); ?>

<?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>


    <?php
         $args = array(
          'post_type' => 'staffs', //カスタム投稿slag
          'orderby' => 'date', //日付を出力する基準
          'order' => 'DESC', //表示する順番(逆はASC)
          'tax_query' => array(//カスタム分類 job_ca の sales に関連付けられた投稿(post)を表示
            array(
              'taxonomy' => 'job_cat',//カスタム分類
              'field'    => 'slug',
              'terms'    => 'sales',//ターム
            ),
          ),
         );

         $the_query = new WP_Query( $args );
         if ( $the_query->have_posts() ) :
         ?>

         <?php 
           while ( $the_query->have_posts() ) : $the_query->the_post();
         //-------- ここから繰り返し---------- 
         ?>

          <?php get_template_part('template-parts/loop', 'staff'); ?>

         <?php //-------- 繰り返しここまで-----------
         endwhile; ?>

         <?php //-------- WP_query終了-----------
                     wp_reset_postdata();      
         endif; ?>




    <?php endwhile; ?>
<?php endif; ?>

<?php get_footer(); ?>

3.ページネーション(WP_QueryでWP Page Nationを使用する場合)

1.WP Page Nationプラグインをインストール 2.WP_Queryを使用する場合は下記を指定

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'paged'=> $paged,
);

3.ページネーションを指定する場所に下記を記述(WP_Queryの場合)

<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(array('query' => $the_query)); } ?>

全体だと下記のようになる

<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<?php get_header(); ?>

<?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>

    <div class="staff-wrap">

        <?php
         $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;//WP-PageNaviでページネーションを作成する場合※WP_Query の場合の記述
         $args = array(
          'paged'=> $paged,//WP-PageNaviでページネーションを作成する場合※WP_Query の場合の記述
          'posts_per_page' => 5,
          'post_type' => 'staffs', //カスタム投稿slag
          'orderby' => 'date', //日付を出力する基準
          'order' => 'DESC', //表示する順番(逆はASC)
          'tax_query' => array(//カスタム分類 job_ca の sales に関連付けられた投稿(post)を表示
            array(
              'taxonomy' => 'job_cat',//カスタム分類
              'field'    => 'slug',
              'terms'    => 'sales',//ターム
            ),
          ),
         );

         $the_query = new WP_Query( $args );
         if ( $the_query->have_posts() ) :
         ?>

         <?php 
           while ( $the_query->have_posts() ) : $the_query->the_post();
         //-------- ここから繰り返し---------- 
         ?>

          <?php get_template_part('template-parts/loop', 'staff'); ?>

         <?php //-------- 繰り返しここまで-----------
         endwhile; ?>

         <?php //-------- WP_query終了-----------
                     wp_reset_postdata();      
         endif; ?>

    </div><!--staff-wrap-->

    <!--WP-PageNaviページネーション ※WP_Query の場合の記述-->
    <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(array('query' => $the_query)); } ?>

    <?php endwhile; ?>
<?php endif; ?>


<?php get_footer(); ?>

投稿ページを作成single-カスタムポスト名.php