Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I was just wondering if anyone could help me out. My pagination isn't working. When I click on the 'next page' link it sends me to a url ending in /page/2/ but gives a 404 error.

I have read tens of questions that are similar but none of the solutions seem to work for me.

Here's my code:

<?php
// Video Category

// calling the header.php
get_header();

// action hook for placing content above #container
thematic_abovecontainer();

?>
<div id="container">
    <?php thematic_abovecontent(); ?>
    <div id="content">
        <?php
            $temp=$wp_query;
            $wp_query=null;
            $wp_query=new WP_Query();
            $wp_query->query('posts_per_page=2&cat=7&paged='.$paged);
            echo '<h1>'; single_cat_title(); echo '</h1>';
            while ($wp_query->have_posts() ) : $wp_query->the_post();?>
                <div>
                    <h2><?php the_title();?></h2>
                    <p><?php the_date();?></p>
                    <div class="video-content"><?php the_content();?></div>
                </div>
            <?php endwhile;?>
            <div id="nav-below" class="navigation">
                <div class="nav-previous"><?php previous_posts_link(); ?></div>
                <div class="nav-next"><?php next_posts_link(); ?></div>
            </div>
            <?php $wp_query = null;
            $wp_query = $temp;
            ?>
    </div><!-- #content -->
 <?php thematic_belowcontent(); ?> 
</div><!-- #container -->

<?php 

// action hook for placing content below #container
thematic_belowcontainer();

// calling the standard sidebar 
thematic_sidebar();

// calling footer.php
get_footer();

?>

Thanks.

share|improve this question
possible duplicate of Pagination not working with custom loop – Chip Bennett Mar 5 '12 at 20:50

closed as too localized by toscho Jul 5 '12 at 21:27

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

Remove $temp = $wp_query; and $wp_query = null.

Then add this between $wp_query=new WP_Query(); and your your call to the query $wp_query->query('posts_per_page=2&cat=7&paged='.$paged);:

global $wp_query, $paged, $numpages;

// How much pages do we have?
$max_page = (int) $wp_query->max_num_pages;

// We need the pagination only if there is more than 1 page
if ( $max_page > (int) 1 )
    $paged = ! $wp_query->query_vars['paged'] ? (int) 1 : $wp_query->query_vars['paged'];

This way your query can recognize if it's currently paged;

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.