繁体   English   中英

wordpress在每个页面中显示相同的帖子

[英]wordpress show same posts in each page pagination

我进行了分页,但在每页显示相同帖子的页面上却不起作用。

码:

<?php if ( have_posts() ) : ?>
    <?php $query = new WP_Query( array('post_type' => 'post', 'kalba' => 'Lietuviu', 'posts_per_page' => 2) );?>
    <?php while ( $query->have_posts() ) : $query->the_post();?>
        <h2 class="title"><a href ="<?php the_permalink();?>"><?php echo $query->post->post_title;?></a></h2>
        <div class="date"><?php echo mysql2date('Y-m-d', $query->post->post_date);?></div>
        <div class="post"><p><?php echo $query->post->post_content;?></p></div>
    <?php endwhile;?>
    <? $big = 999999999; // need an unlikely integer
        echo paginate_links( array( 'base'    => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
                            'format'  => '?paged=%#%',
                            'current' => max( 1, get_query_var( 'paged' ) ),
                            'total'   => $query->max_num_pages,
                            'end_size'=> 1,
                            'mid_size'=> 10 ) );?>
<?php endif; ?>

我找不到问题,也许问题是我使用了“分类法”。

问题是您一次又一次地执行相同的查询:

<?php $query = new WP_Query( array('post_type' => 'post', 'kalba' => 'Lietuviu', 'posts_per_page' => 2) );?>

您需要在WP_Query数组中设置偏移量'offset'=> 2

我们可以使用一个简单的公式,使用wordpress使用的称为$page的全局变量来计算偏移量: $offset = ($page -1) * $post_per_page;

因此您的最终WP_QUERY应该如下所示:

<?php $query = new WP_Query( array('post_type' => 'post', 'kalba' => 'Lietuviu', 'posts_per_page' => 2, 'offset'=> (($page -1) * 2) ) );?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM