页面上的WordPress分页

时间:2010-07-14 06:49:51

标签: wordpress

我在WordPress页面上有以下代码。它基本上只抓取3个帖子并显示它们以及页面内容本身。我想添加的是分页,以便用户可以浏览所有帖子,我如何使用这样的自定义循环?

    <?PHP

        get_header();

        /* Template Name: News */

    ?>

    <div style="padding: 0 20px;">


        <div class="box clearfix side" style="margin:10px 0;">

        <div style="float:left;width:628px;">

        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                <div class="content" id="post-<?php the_ID(); ?>">
                    <h2><?php the_title(); ?><?php edit_post_link('Edit', ' <small>[', ']</small>'); ?></h2>

        <?php the_content('<p>Read the rest of this page &raquo;</p>'); ?>


                    <?php wp_link_pages(array('before' => '<p>Pages: ', 'after' => '</p>', 'next_or_number' => 'number')); ?>


                    <hr />

                </div>
                <?php endwhile; endif; ?>

<hr />

        <?php $blog_query = new WP_Query('posts_per_page=3'); while ($blog_query->have_posts()) : $blog_query->the_post(); ?>

            <div class="content" id="post-<?php the_ID(); ?>">

                <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>



                <?php the_excerpt(); ?>

            </div>



        <?php endwhile; ?>

        <?php if ($blog_query->have_posts()) : ?>

            <?php if(function_exists('wp_pagenavi'))
            {
                wp_pagenavi();
            }
            ?>

                <?php else: ?>

                <h2>oooops!!!</h2>


                <?php endif; ?>


        </div>


        </div>

    </div>

    <?PHP

        get_footer();

    ?>

2 个答案:

答案 0 :(得分:0)

你确定你不是在这里重新发明轮子吗?为什么不设置要在admin中显示的帖子数,然后使用WP的本地分页来进行博客?

答案 1 :(得分:0)

原则上你必须做这样的事情:

<?php $temp = $wp_query; $wp_query= null; ?>        

        <?php $wp_query = new WP_Query(array('posts_per_page' => 3, 'paged' => $paged)); while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

            <div class="content" id="post-<?php the_ID(); ?>">

                <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>

                <?php the_excerpt(); ?>

            </div>

        <?php endwhile; ?>

        <?php if(function_exists('wp_pagenavi'))
        {
            wp_pagenavi();
        }
        ?>

        <?php $wp_query = null; $wp_query = $temp; ?>