Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

For some reason the below code works until I hit the third page and gives 404. Is it possible to fix it without using any plugins?

<?php
    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query('cat='. $id .'&posts_per_page=4&paged='.$paged);
    while ($wp_query->have_posts()) : $wp_query->the_post();
?>

<?php get_the_content() ?>

<?php endwhile; ?>


<?php if (  $wp_query->max_num_pages > 1 ) : ?>
    <?php previous_posts_link( __( 'Previous', 'themename' ) ); ?>
    <?php next_posts_link( __( 'Next', 'themename' ) ); ?>
<?php endif; ?>

<?php $wp_query = null; $wp_query = $temp;?>
share|improve this question
    
why not create a new instance of $posts array? like $new_query = new WP_Query(' ... and i think you need to use wp_reset_query(); after the custom WP_Query. –  Bainternet Feb 3 '11 at 22:37
    
I tried that and it didn't work. –  Eeyore Feb 4 '11 at 4:31

2 Answers 2

up vote 1 down vote accepted

Try creating a new instance of WP_Query and at the end use wp_reset_query(); like this:

<?php
    $my_query = new WP_Query();
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $my_query->query('cat='. $id .'&posts_per_page=4&paged='.$paged);
    while ($my_query->have_posts()) : $my_query->the_post();
?>

<?php get_the_content() ?>

<?php endwhile; ?>


<?php if (  $my_query->max_num_pages > 1 ) : ?>
    <?php previous_posts_link( __( 'Previous', 'themename' ) ); ?>
    <?php next_posts_link( __( 'Next', 'themename' ) ); ?>
<?php endif; ?>

<?php wp_reset_query(); ?>
share|improve this answer
    
Yes, do not use $wp_query = null, use wp_reset_query(). –  Ciprian Jan 4 '12 at 19:27

Here is some working code i use:

    <?php $temp_query = $wp_query;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("category_name=".$cat."&showposts=20&paged=".$paged."");

    while (have_posts()) : the_post(); ?>

        the_content();

    endwhile; ?>

    <div class="navigation2">
        <div class="navleft"><?php next_posts_link('Older Entries') ?></div>
        <div class="navright"><?php previous_posts_link('Newer Entries') ?></div>
    </div>

    <?php $wp_query = $temp_query; ?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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