根据分页页码显示帖子的循环

时间:2013-03-12 08:06:37

标签: php wordpress pagination

我正在为wordpress帖子创建一个分页系统,以每页8个帖子的顺序显示,而不是在每个页面中显示第一个结果。为此,我制作了这段代码

<?php $offset_wordp=mysql_escape_string($_GET['page']); ?>
    <? query_posts ('cat=-103&posts_per_page=8&offset='.$offset_wordp.'') ?>

 <? while (have_posts ()) { the_post ();  ?>

    <div class="reu-sec">

        <h2><a href="<? the_permalink() ?>"><? the_title (); ?><span class="sbttlmn">&nbsp<?= get_post_meta (get_the_ID(), 'sub-title', true); ?></span></a></h2>

       <div style="clear: both"></div>

    </div>

    <? } ?>
</div>

在上面的代码中,我使用offset来过滤那些帖子后的结果。我尝试的是得到page number,然后相应地过滤偏移,例如。如果页码为1,则offset=1,如果页码为2,则偏移量为8,依此类推。因此,该帖子会根据页码自动过滤。

1 个答案:

答案 0 :(得分:0)

第一页的偏移量为0.从第二页开始,偏移量将增加8,使用以下公式计算偏移量。

$offset = ($page_num == 1)? 1 : ($page_num - 1) * 8;