A
A
Anonimmus2021-08-20 12:15:29
WordPress
Anonimmus, 2021-08-20 12:15:29

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(); ?>


611f72977b30f574377985.png
611f729f29950973887220.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Zolin, 2021-08-21
@Anonimmus

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 question

Ask a Question

731 491 924 answers to any question