K
K
Konstantin2018-02-22 18:25:35
WordPress
Konstantin, 2018-02-22 18:25:35

How to solve the problem of pagination when displaying certain posts on the main page?

I display on the Main only posts of a certain category (blog). But after going to page 2, there are the same posts. Here is the code:

<?php	

if (have_posts()) :
   
    query_posts('cat=1');
    
  while (have_posts()) : the_post(); ?>
  

  
   	<article class="post">
          
      <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

      <div class="post__info">
                     <span class="post__info--date"><?php echo get_the_date(); ?></span>
                     <span class="post__info--comments"><a href="<?php the_permalink(); ?>#comments">комментировать</a></span>
            </div>

      <?php the_content(); ?>
      
      <p class="post__hashtag"><?php the_tags($before=""); ?></p>
      
    

    </article>

  <?php endwhile;
  
  
  
  else :
    echo '<p>Записей нет</p>';
  
  endif;
  

  
  ?>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
Orkhan Hasanli, 2018-02-22
Kapelnikov @Kapelnikov

It's better to use WP_Query instead of query_posts.
And you can display posts with pagination like this:

<?php global $wp_query;
                $wp_query = new WP_Query(array(
                  'posts_per_page' => '4',
                  'post_type' => 'post',
                                                                        'category__in' => '1', //дочерние будут проигнорированы
                  'paged' => get_query_var('paged') ?: 1 // страница пагинации
                ));
                while( have_posts() ) { the_post(); ?>
<!-- content здесь -->
              <?php }  ?>
              <!-- пагинация здесь -->
              <?php wp_reset_query(); ?>

An example with such an implementation: https://md7.info/fakty

D
Dima, 2018-02-22
Dolgoter @DDolgy

As far as I understand, they are displayed in the sidebar?

K
Konstantin Teploukhov, 2018-02-22
@blood-moon

Most likely a fierce crutch and in general they can kill for this
On the main page, for example, you display 4 posts and add the offset=4 parameter to the next page in the cycle. Tb skip 4 posts that were displayed on the main page. here it is https://wp-kama.ru/function/wp_query

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question