Answer the question
In order to leave comments, you need to log in
How to show pagination on category page?
Hello.
I'm trying to display all the posts related to this category in the category-news.php file, but something doesn't add up.
if you turn it into a page, the pagination works, but the rubric, alas, does not work - the page is not found writes.
<!-- Шаблон категории - новости -->
<? get_header(); ?>
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 2,
'paged' => $paged,
'page' => $paged,
);
// тут код вывода записей
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<div class="news__item">
<div class="news__top">
<p class="news__category">Новости</p>
</div>
<div class="news__image"><?php the_post_thumbnail('news-preview', ''); ?></div>
<a href="<?php the_permalink(); ?>"><h3 class="news__title"><?php the_title(); ?></h3></a>
</div>
<?php endwhile;
endif; ?>
<?php
wp_reset_postdata();
the_posts_pagination( );
?>
<? get_footer(); ?>
Answer the question
In order to leave comments, you need to log in
There is already a global query on the archive pages of categories and taxonomies, interrupting it with your own is bad manners and a mockery of performance. I
recommend that you look at archive.php of popular or standard twenty themes, you will not find custom queries there. WP_Query()
With a global query, core functions work fine the_posts_pagination()
or the_posts_navigation()
But in rare In some cases, you really may need to paginate a custom request, in which case you can use the example from this article.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question