WordPressの固定ページでwp-pagenaviを使うときにはまったのでメモ…
固定ページで記事一覧ページを作るときに、ページネーションのプラグイン『wp-pagenavi』が機能しなかったので
色々調べて正解?にたどり着いたのでメモします。
//queryをセット
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 8,
'paged' => $paged,
'post_type' => array(
'post',
),
);
$the_query = new WP_Query($args);
?>
//記事ループの部分
<?php if($the_query->have_posts()): ?>
<?php while($the_query->have_posts()) : $the_query->the_post(); ?>
<?php the_post_thumbnail('thumbnail'); ?>
<?php the_category(); ?>
<?php the_permalink(); ?><?php the_title(); ?>
<?php echo get_the_date('Y年m月d日'); ?>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php endif; ?>
//ページナビ
<?php if(function_exists('wp_pagenavi')){wp_pagenavi(array('query'=>$the_query));} ?>
//query リセットを忘れずに。
<?php wp_reset_postdata(); ?>
1行目の
<?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; ?>
これを入れないと2ページ目に移行しないです。
ページナビのコードの部分は
<?php if(function_exists('wp_pagenavi')){wp_pagenavi(array('query'=>$the_query));} ?>
引数に$queryを入れます。
普通に使えると思ってたのですが見事に2,3時間溶かしました。
結構使いそうなので次回からは大丈夫と思う。