Issue
I have a blog start page that displays several blocks of articles.
- The first block is the output of the last published article
- The second block is the output of articles from the 2nd to the 7th article
- The third block is the output of articles starting from the 8th article and pagination is connected to it
If I click "go to the next page", then everything that was on the start page is displayed on it, and I need to display the feed of posts (12 posts) starting from the second page, starting from the 14th post, and on each subsequent page just continue to display all posts in turn.
Tell me, is there a solution to this problem and how best to implement it?
Post output code:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'paged' => $paged,
'offset' => 7,
'posts_per_page' => 6,
);
query_posts($args);
?>
Pagination output code:
<?php endwhile ; ?>
<div class="list-item">
<!--paginate-->
<?php the_posts_pagination (
$args = array(
'show_all' => false,
'end_size' => 0,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => __('Previous'),
'next_text' => __('Next'),
'add_args' => false,
'add_fragment' => '',
'screen_reader_text' => __( 'Posts navigation' ),
'type' => 'list',
)
);
?>
</div>
<?php else : ?>
<?php endif ; ?>
Solution
Here is my answer. In this case, I created 4 cycles that display the required number of posts in different blocks of the page. The fifth cycle was also created, which starts working from the moment we switched to the second page of posts and further.
For pagination, I used the_posts_pagination
as it was required in my technical specification and in order for it to work correctly I used the global variable WP_Query
<?php
if(!get_query_var('page')) {
?>
<div class="wrap-blog-home-page">
<?php
$args = array(
'posts_per_page' => 1,
'paged' => (get_query_var('page')) ? get_query_var('page') : 1
);
$queryLasPost = new WP_Query( $args );
?>
<?php
if ( $queryLastPost->have_posts()):
while ( $queryLastPost->have_posts() ) : $queryLastPost->the_post();
?>
<!--posts--->
<?php endwhile ; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<?php endif ; ?>
<section class="bodyContent">
<?php
$args = array(
'offset' => 1,
'posts_per_page' => 6,
);
$queryFirstBlock = new WP_Query( $args );
?>
<?php
if ( $queryFirstBlock->have_posts()):
while ( $queryFirstBlock->have_posts() ) : $queryFirstBlock->the_post();
?>
<!--posts--->
<?php endwhile ; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<?php endif ; ?>
<section class="bodyContent">
<?php
$args = array(
'offset' => 8,
'posts_per_page' => 6,
'paged' => (get_query_var('page')) ? get_query_var('page') : 1
);
$querySecondBlock = new WP_Query( $args );
?>
<?php
if ( $querySecondBlock->have_posts()):
while ( $querySecondBlock->have_posts() ) : $querySecondBlock->the_post();
?>
<!--posts--->
<?php endwhile ; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<?php endif ; ?>
</section>
<section class="bodyContent">
<?php
$args = array(
'posts_per_page' => 12,
'paged' => (get_query_var('page')) ? get_query_var('page') : 1
);
$queryThirdBlock = new WP_Query( $args );
?>
<?php
if ( $queryThirdBlock->have_posts()):
while ( $queryThirdBlock->have_posts() ) : $queryThirdBlock->the_post();
?>
<!--posts--->
<?php endwhile ; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<?php endif ; ?>
</section>
<?php
} else {
?>
<section class="bodyContent">
<?php
$args = array(
'posts_per_page' => 12,
'paged' => (get_query_var('page')) ? get_query_var('page') : 1
);
$queryThirdBlock = new WP_Query( $args );
?>
<?php
if ( $queryThirdBlock->have_posts()):
while ( $queryThirdBlock->have_posts() ) : $queryThirdBlock->the_post();
?>
<!--posts--->
<?php endwhile ; ?>
<?php wp_reset_postdata(); ?>
<!-- nextpage -->
<?php else : ?>
<?php endif ; ?>
</section>
<?php
}
?>
<section class="bodyContent">
<?php
global $wp_query;
$restore_wp_query = $wp_query;
$wp_query = $queryThirdBlock;
?>
<div class="list-item">
<!--paginate-->
<?php the_posts_pagination (
$args = array(
'show_all' => false,
'end_size' => 0,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => __('Previous'),
'next_text' => __('Next'),
'add_args' => false,
'add_fragment' => '',
'screen_reader_text' => __( 'Posts navigation' ),
'type' => 'list',
)
);
?>
<?php
$wp_query = $restore_wp_query;
?>
</div>
</section>
</div>
<?php get_footer(); ?>
Answered By - Slava Nikitina
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.