I am creating category and tag page templates for my client so they can post news articles to the news category page and specific news pages separated by tag with a pagination nav at the bottom of each page. What I noticed, however, is the pagination nav takes account of all of the posts belonging to any category or having any tag.
I am aware that paginate_links() does not pass an argument in any way similar to the category_name argument for WP_Query(). Keeping this in mind, I spent Googling for a solution to excluding all of the irrelevant posts from the scope of paginate_links() with no luck.
Can you write paginate_links() so that the function targets a specific category or tag? If so, please help me work out how.
The below is the code including the query and pagination function:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'post_status'=>'publish',
'category_name' => 'NEWS',
'tag_slug__and' => $tags,
'posts_per_page' => 2,
'paged' => $paged
);
$the_query = new WP_Query($args); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="inner">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<div class="pagination">
<?php
echo paginate_links( array(
'format' => 'page/%#%',
'current' => $paged,
'total' => $the_query->max_num_pages,
'mid_size' => 2,
'prev_text' => __('« Previous Page’),
'next_text' => __(‘Next Page »')
) );
?>
</div>
<?php endif; ?>
Thank you for reading this.
Ead
I am creating category and tag page templates for my client so they can post news articles to the news category page and specific news pages separated by tag with a pagination nav at the bottom of each page. What I noticed, however, is the pagination nav takes account of all of the posts belonging to any category or having any tag.
I am aware that paginate_links() does not pass an argument in any way similar to the category_name argument for WP_Query(). Keeping this in mind, I spent Googling for a solution to excluding all of the irrelevant posts from the scope of paginate_links() with no luck.
Can you write paginate_links() so that the function targets a specific category or tag? If so, please help me work out how.
The below is the code including the query and pagination function:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'post_status'=>'publish',
'category_name' => 'NEWS',
'tag_slug__and' => $tags,
'posts_per_page' => 2,
'paged' => $paged
);
$the_query = new WP_Query($args); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="inner">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<div class="pagination">
<?php
echo paginate_links( array(
'format' => 'page/%#%',
'current' => $paged,
'total' => $the_query->max_num_pages,
'mid_size' => 2,
'prev_text' => __('« Previous Page’),
'next_text' => __(‘Next Page »')
) );
?>
</div>
<?php endif; ?>
Thank you for reading this.
Ead
Share Improve this question edited Jun 13, 2019 at 4:43 Sally CJ 40.1k2 gold badges28 silver badges49 bronze badges asked Jun 12, 2019 at 6:17 FizzlerFizzler 757 bronze badges 14 | Show 9 more comments1 Answer
Reset to default 1The tag template page began displaying posts for specific tags once I replaced the argument statement with the following, which is great!!:
$tags = get_the_tags();
$tag = $tags[0]->name;
$args = array(
'post_type' => 'post',
'post_status'=>'publish',
'category_name' => 'NEWS',
'tag_slug__and' => $tag,
'posts_per_page' => 2,
'paged' => $paged
);
However, when I click on a pagination link on the tag archive page, Chrome returns 'This localhost page can’t be found'...
paginate_links()
. Custom query will suffice I guess. See this example - developer.wordpress/reference/functions/paginate_links/… – Nilambar Sharma Commented Jun 12, 2019 at 6:31paginate_links()
only paginate the query's results; you can use thepre_get_posts
hook (particularly for the main query) to control the query arguments or make a customWP_Query
request where you have full control over the query arguments. So for example, you can donew WP_Query( 'category_name=foo' )
and then callpaginate_links()
, and the pagination wouldn't "include" posts that are not in thefoo
category. – Sally CJ Commented Jun 12, 2019 at 6:32$the_query = new WP_Query($args);
? That should not be required in category templates. – Jacob Peattie Commented Jun 12, 2019 at 7:24