Answer the question
In order to leave comments, you need to log in
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
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(); ?>
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 questionAsk a Question
731 491 924 answers to any question