投稿一覧で子カテゴリーの投稿を除外して表示する際のページネーションの設定です。
ほぼ、下記URLの本家の設定そのまま引用して下記サンプルをコピペすればそのまま使用できるようにしました。
引用元=https://codex.wordpress.org/Function_Reference/paginate_links?diff=cur&oldid=151763
<?php
$term_id = get_query_var('cat');//現在の投稿のカテゴリーIDを取得
$taxonomy_name = 'category';
$termchildren = get_term_children( $term_id, $taxonomy_name );//子カテゴリーを配列で取得
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;//ページネーション用
$the_query = new WP_Query(
array(
'paged' => $paged,
'post_type' => 'post',
'cat' =>$term_id,
'order' => 'desc',
'category__not_in' => $termchildren
)
);
?>
<ul>
<?php //ループ
if($the_query->have_posts()): while($the_query->have_posts()): $the_query->the_post();
?>
<li><?php echo get_the_title(); ?></li>
<?php endwhile; endif; wp_reset_postdata(); ?>
</ul>
<?php //ページネーション表示
$big = 999999999; // need an unlikely integer
echo paginate_links(
array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages,
'prev_text' =>('«'),
'next_text' =>('»'),
'end_size' =>1,
'mid_size' =>2
)
);
?>
ページネーション表示部分をfunctions.phpに記載しておけば、簡単に流用できます。
その際は、変数をグローバル変数にする必要があります。
//functions.phpに記載
function custom_paginate(){
global $the_query; //グローバル変数
$big = 999999999; // need an unlikely integer
echo paginate_links(
array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages,
'prev_text' =>('«'),
'next_text' =>('»'),
'end_size' =>1,
'mid_size' =>2
)
);
}
//下記を表示するテンプレートに記載
<?php custom_paginate(); ?>