更新 2020 / 10 / 05

カスタム投稿のarchive-○○.phpでwp_pagenaviが動かない場合

テンプレートには通常のhave_postsと<?php wp_pagenavi(); ?>を書く。

functions.phpにposts_per_pageなどを記述して表示数を取得するのみ。

archive-custom.php


                  <ul>
                  <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                  <li>
                  <?php if ( has_post_thumbnail() ): ?>
                  <?php the_post_thumbnail( 'full' ); ?>
                  <?php else: ?>
                  <img src="<?php bloginfo( 'template_url' ); ?>/img/common/thumbnail-default.jpg" alt="〝no img">
                  <?php endif; ?>
               
                  <?php the_title();?>
                  <a class="btn_purple cen" href="<?php the_permalink(); ?>">詳しくはこちら</a>
                  </li>
                  <?php endwhile; ?>
                  <?php endif; ?>
                  </ul>
                  <?php wp_pagenavi(); ?>
                

function.php


                  //カスタム投稿のページャー
                function change_posts_per_page($query) {
                    if ( is_admin() || ! $query->is_main_query() ){
                         return;
                    }

                    if ( $query->is_post_type_archive( 'voice' ) ) {
                         $query->set( 'posts_per_page', '12' );
                         return;
                    }

                }
                add_action( 'pre_get_posts', 'change_posts_per_page' );