Exclude a post category from pagination
If you want a certain category to be excluded from the pagination of your posts page just follow the steps below:
- Login to your WordPress site as an administrator
- Select “Appearance” and “Theme Editor”
- At the “Theme Files” (right sidebar) open the “template-parts” droplet and select “content-single.php”
- Find the “the_post_navigation” function and add the following line before that
$category = get_category_by_slug('slug_of_my_post_category');
- Inside the “the_post_navigation” function add the following line just before the first row
'excluded_terms' => $category->term_id,
The whole code snippet should look like this
$category = get_category_by_slug('slug_of_my_post_category');
the_post_navigation(array(
'excluded_terms' => $category->term_id,
'next_text' => '<span class="meta-nav" aria-hidden="true">' . esc_html__('Next:', 'highlight') . '</span> ' .
'<span class="screen-reader-text">' . esc_html__('Next post:', 'highlight') . '</span> ' .
'<span class="post-title">%title</span>',
'prev_text' =>
'<span class="meta-nav" aria-hidden="true">' . esc_html__('Previous:', 'highlight') . '</span> ' .
'<span class="screen-reader-text">' . esc_html__('Previous post:', 'highlight') . '</span> ' .
'<span class="post-title">%title</span>',
));
Click on the “Update File” button to save your changes. Reload the post’s page from where you want to exclude the specific category. Also remember to replace the “slug_of_my_post_category” with the slug ID of the category you want to exclude. In case you want more post categories to be excluded you may apply a string of those IDs (you can find each ID when hovering your mouse over each post category at the corresponding page) separated by commas (e.g. ‘excluded_terms’ => ’13, 14′)