wordpress4.9.6
カスタム投稿で"blog"を作成しています。
archive-blog.phpテンプレートを作成し、
投稿記事の一覧をページングで表示させたいと思っています。
arvhive-blog.php
<?php get_header(); ?> <?php $args = array( 'post_type' => 'blog', 'ignore_sticky_posts' => 1, 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => 2, ); $my_query = new WP_Query( $args ); if ( $my_query->have_posts() ): while ( $my_query->have_posts() ): $my_query->the_post(); ?> <article class="blog-item"> <p class="date"> <?php the_time('Y.m.d') ?> </p> <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> <div> <?php the_content(); ?> </div> </article> <?php endwhile; ?> <?php wp_reset_query();endif; ?> <?php if(function_exists('wp_pagenavi')) wp_pagenavi(array('query' => $my_query));?> <?php get_footer();
こんなかんじなのですが、
/blog/は問題ないのですが、
次のページ(2)を押し
/blog/page/2/
にいくとnot foundになります。
いろいろこの件の記事がたくさんでているのですが、
どうも条件が違うのか改善されなかったり
wordpress自体が見れなくなってしまったりと・・・
ご教授いただけますと幸いです。
/解決方法/
function.php
function change_posts_per_page($query) { if ( is_admin() || ! $query->is_main_query() ){ return; } if ( $query->is_post_type_archive( 'blog' ) ) { $query->set( 'posts_per_page', '3' ); return; } } add_action( 'pre_get_posts', 'change_posts_per_page' );
archive-blog.php
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <article class="blog-item"> <p class="date"> <?php the_time('Y.m.d') ?> </p> <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> <div> <?php the_content(); ?> </div> </article> <?php endwhile; ?> <?php endif; ?> <?php wp_pagenavi(); ?>
上記で無事解決しました!
回答1件
あなたの回答
tips
プレビュー