0

I have tried to add pagination in the below code but dont know how to integrate with it. Kindly help to get the paginations on the posts.

<?php 
                        $child_query = new WP_Query(array('post_type' => 'blogpost', 'orderby' => 'date', 'order' => 'DESC'));
                        while ( $child_query->have_posts() ) : $child_query->the_post(); ?>
                            <div class="single-post">
                                <div class="leftContent">
                                    <?php 
                                        $image = get_the_post_thumbnail($post->ID,array(300,180)); 
                                        if($image == ''){
                                            $image = wp_get_attachment_image(11270,array(300,180));
                                        }
                                    ?>
                                    <a class="title" href="<?php the_permalink()?>"><?php echo $image; ?></a>
                                    
                                </div>
                                <div class="rightContent">
                                    <a class="title" href="<?php the_permalink()?>"><?php the_title()?></a><br>
                                    <div class="post-date"><?php the_time('F jS, Y') ?> by <?php echo get_the_author() ?></div>

                                    <div class="content">
                                        <?php echo excerpt($post->ID,320); ?>
                                        
                                    </div>
                                    <a href="<?php the_permalink()?>" class="readMore" >Read More</a>
                                </div>
                            </div>
                            
                    <?php 
                        endwhile;
                    ?>
3
  • 1
    check Pagination when using wp_query? – Buttered_Toast Feb 10 at 12:13
  • This just looks like the archive of latest blogpost posts. Is there a reason you’re using a custom query and not just the archive template? – Jacob Peattie Feb 10 at 12:25
  • @JacobPeattie I am not sure that this is the code which is on blog page template, need to intrgrate the pagination but it is not coming. I have tried what the above comment says so the pagination came but when i change to next showing same contents only – Naveen Feb 10 at 12:29
0

Fixed by adding the below code.

<?php 
                        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                            $child_query = new WP_Query(array('post_type' => 'blogpost', 'orderby' => 'date', 'order' => 'DESC','paged' => $paged));
                            while ( $child_query->have_posts() ) : $child_query->the_post(); ?>
                                <div class="single-post">
                                    <div class="leftContent">
                                        <?php 
                                            $image = get_the_post_thumbnail($post->ID,array(300,180)); 
                                            if($image == ''){
                                                $image = wp_get_attachment_image(11270,array(300,180));
                                            }
                                        ?>
                                        <a class="title" href="<?php the_permalink()?>"><?php echo $image; ?></a>
                                        
                                    </div>
                                    <div class="rightContent">
                                        <a class="title" href="<?php the_permalink()?>"><?php the_title()?></a><br>
                                        <div class="post-date"><?php the_time('F jS, Y') ?> by <?php echo get_the_author() ?></div>

                                        <div class="content">
                                            <?php echo excerpt($post->ID,320); ?>
                                            
                                        </div>
                                        <a href="<?php the_permalink()?>" class="readMore" >Read More</a>
                                    </div>
                                </div>
                                
                        <?php 
                            endwhile;
                        ?>
                        <div class="pagination">
                        <?php 
                            echo paginate_links( array(
                                'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
                                'total'        => $child_query->max_num_pages,
                                'current'      => max( 1, get_query_var( 'paged' ) ),
                                'format'       => '?paged=%#%',
                                'show_all'     => false,
                                'type'         => 'plain',
                                'end_size'     => 2,
                                'mid_size'     => 1,
                                'prev_next'    => true,
                                'prev_text'    => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
                                'next_text'    => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
                                'add_args'     => false,
                                'add_fragment' => '',
                            ) );
                        ?>
                    </div>
                    <?php wp_reset_postdata(); ?>
                    </div>  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.