1

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;?>
  • 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
1

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(); ?>
  • Yes, do not use $wp_query = null, use wp_reset_query(). – Ciprian Jan 4 '12 at 19:27
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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