カスタム投稿で投稿タイプnewsを作成しました。
archive-news.phpにて通常投稿とnewsタイプの投稿を10件取得し、それ以降はページ送りとしたいのですが、
2ページ目のリンクを押すと何も表示されません。
php
1<?php get_header(); ?> 2<div class="containerArea"> 3 4<article <?php post_class( 'kiji' ); ?>> 5<!-- タイトル --> 6<?php if(is_category() || is_tag()): ?> 7 <h1><?php single_cat_title() ?>一覧</h1> 8<?php elseif(is_year()): ?> 9 <h1><?php the_time("Y年") ?>の記事一覧</h1> 10<?php elseif(is_month()): ?> 11 <h1><?php the_time("Y年m月") ?>の記事一覧</h1> 12 <?php elseif(is_author()): ?> 13 <h1><?php the_author() ?>の記事一覧</h1> 14 <?php else: ?> 15 <h1>新着記事一覧</h1> 16<?php endif; ?> 17 18<?php 19$paged = (int) get_query_var('paged'); 20$args = array( 21'paged' => $paged 22 23); 24$the_query = new WP_Query($args); 25?> 26<div class="info-list"> 27<?php 28if ( $the_query->have_posts() ) : 29 while ( $the_query->have_posts() ) : $the_query->the_post(); 30?> 31 32 33 <div class=" first-info"> 34 <div class="info"> 35 <div class="one-news"> 36 <span class="index-category"> 37 <?php echo get_the_category_list( ' ' ); ?> 38 </span> 39 <span class="author"> <?php the_author_posts_link();?></span> 40 41 <time datetime="<?php echo get_the_date( 'Y-m-d' ); ?>"> 42 <?php echo get_the_date(); ?> 43 </time> 44 <a class="title-link" href="<?php the_permalink(); ?>"> 45 <?php the_title(); ?> 46 </a> 47 </div> 48 </div> 49 </div> 50<?php endwhile; endif; ?><!--ループ終了--> 51</div> 52 53 54 <div class="pagenation"> 55 56 57<?php 58if ($the_query->max_num_pages > 1) { 59 echo paginate_links(array( 60 'base' => get_pagenum_link(1) . '%_%', 61 'format' => 'page/%#%/', 62 'current' => max(1, $paged), 63 'prev_next' => True, 64 'prev_text' => __('«'), 65 'next_text' => __('»'), 66 'total' => $the_query->max_num_pages 67 )); 68} 69?> 70</div> 71 72</article> 73<?php // endif; ?> 74 75</div> 76<?php get_footer(); ?> 77
管理画面の表示設定にて「1ページに表示する最大投稿数」を10から1にすると、ページ送りがうまく行きました。
ただ、こうすると他のカテゴリページの表示が1件になってしまいます。
何となくですが、カスタム投稿のnewsが「1ページに表示する最大投稿数」にカウントされてない気がしています。
pre_get_postsを試して見たのですが、この場合はarchive-news.phpが機能せず、index.phpで一覧が表示されるようになってしまいました。
php
1function change_posts_per_page($query) { 2 /* 管理画面,メインクエリに干渉しないために必須 */ 3 if( is_admin() || ! $query->is_main_query() ){ 4 return; 5 } 6 7if( $query->is_post_type_archive( 'news' ) ){ 8 $query->set( 'post_type',array( 'post','news' ) ); 9 $query->set( 'posts_per_page', '10' ); 10 return; 11 } 12 13} 14add_action( 'pre_get_posts', 'change_posts_per_page' ); 15
この場合、どのような解決方法がございますでしょうか。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。