开发者

WordPress Pagination on Page

开发者 https://www.devze.com 2023-01-07 06:19 出处:网络
I have the following code on a WordPress page. It basically just grabs 3 posts and displays them as well as the page content itself up top. What I want to add is pagination so that a user can flick th

I have the following code on a WordPress page. It basically just grabs 3 posts and displays them as well as the page content itself up top. What I want to add is pagination so that a user can flick through all the posts, how do I get this working with custom loops like this?

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

    ?>


Are you sure you're not re-inventing the wheel a little here? Why not set the number of posts to display in the admin, then use WP's native paging for the blog?


Turns out you have to do something like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消