tags:

views:

33

answers:

2

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

    ?>
A: 

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?

TheDeadMedic
Doesn't matter if I set the number in the admin or the code, and use the native pagination. The problem is that it's not showing up or when I manually type in the /page/2/ in the url it's not showing the next page of posts. So something is up, but what?
Cameron
What is it showing? 404? Is this a page template or the standard `index.php`? Pagination will only work on a standard `have_posts()` loop inside `index.php`
TheDeadMedic
it's just showing the same content so essentially it's ignoring the url, but it's also not showing the pagination!
Cameron
Switch to the default theme (TwentyTen with 3.0), and see if everything is working OK. Have you set a static front page in your reading settings?
TheDeadMedic
A: 

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; ?>
Cameron
Yeah so basically you have to declare the global wp_query and set it inside a temp va. Pretty sucky :/
Cameron