Add pagination to WP_Query

时间:2021-02-10 作者:Naveen

我试图在下面的代码中添加分页,但不知道如何与之集成。请帮忙在帖子上找到页码。

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

1 个回复
SO网友:Naveen

通过添加以下代码进行修复。

<?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>  

相关推荐

WooCommerce Custom Pagination

有人能帮我解决我的定制归档产品的问题吗。php?我正在尝试将分页添加到我制作的网格中,但不知道从何处开始。这是我的产品代码:<?php $the_query = new WP_Query( array( \'posts_per_page\'=>12, \'post_type\'=>\'product\', \'paged\' => get_query_var(\'paged\') ? get_query_var(\'paged\') : 1