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.

My pagination pages echo out the same content on every page. Page one is queried to show two posts, which it shows up correctly. Page two shows the exact same two posts as page one does. Here is my code: (functions.php) (this is a pre-made code I found on the web).

function wp_corenavi() {
  global $wp_query, $wp_rewrite;
  $pages = '';
  $max = $wp_query->max_num_pages;
  if (!$current = get_query_var('paged')) $current = 1;
  $a['base'] = str_replace(999999999, '%#%', get_pagenum_link(999999999));
  $a['total'] = $max;
  $a['current'] = $current;

  $total = 1; //1 - display the text "Page N of N", 0 - not display
  $a['mid_size'] = 5; //how many links to show on the left and right of the current
  $a['end_size'] = 1; //how many links to show in the beginning and end
  $a['prev_text'] = '« Previous'; //text of the "Previous page" link
  $a['next_text'] = 'Next »'; //text of the "Next page" link

  if ($max > 1) echo '<div class="navigation">';
  if ($total == 1 && $max > 1) $pages = '<span class="pages">Page ' . $current . ' of ' . $max . '</span>'."\r\n";
  echo $pages . paginate_links($a);
  if ($max > 1) echo '</div>';
}

(Custom Page Template)

<?php

get_header(); 

if ( have_posts() ) : ?>
        <div id="container">
            <div id="content" role="main">

                <?php query_posts( 'category_name=category&showposts=2'); ?>
                <?php while ( have_posts() ) : the_post(); ?>

                            <div>
                                <div>
                                    <div class="thumb">
                                      <?php the_post_thumbnail('thumbnail'); ?> 
                                   </div>
                                    <h2 class="entry-title" style="text-align:center;"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                                    <div class="the-excerpt">
                                        <?php echo content(15, __('(Read More)')); /* twentyten_posted_on(); */ ?>
                                    </div><!-- .entry-meta -->
                                </div>
                            </div>

                <?php endwhile; // end of the loop. ?>
                <br clear="all" />
                <?php if (function_exists('wp_corenavi')) wp_corenavi(); ?>
            </div><!-- #content -->
        </div><!-- #container -->
<?php endif; ?>
share|improve this question

2 Answers 2

up vote 0 down vote accepted

This is because query_posts resets the query. See this warning on the Codex page:

Pagination won't work correctly, unless you use query_posts() in a page template and you set the 'paged' query var appropriately: http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html

The call:

query_posts( 'category_name=category&showposts=2');

does not tell it which page to get (so it gets the first page). It's a simple matter of:

$page = get_query_var('paged');
$page = (!empty($page) ? $page : 1);
query_posts( 'category_name=category&showposts=2&paged='.$page);

Note: For pages (i.e. when using a custom page template, you need to use get_query_var('page') rather than get_query_var('paged').

share|improve this answer
    
I'm getting the error: Can't use function return value in write context in.... get_header(); $page = (!empty(get_query_var('page')) ? get_query_var('page') : 1); query_posts( 'category_name=category&showposts=2&paged='.$page); if ( have_posts() ) : ?> –  Stephen Mar 13 '12 at 20:31
    
Corrected answer. Also see Chip's comments about if ( have_posts() ) : and using WP_Query, rather than query_posts. :D –  Stephen Harris Mar 13 '12 at 20:46
    
Not sure I understand Stephen? –  Stephen Mar 13 '12 at 21:06
    
Sorry, not sure what happened. I've updated the answer. –  Stephen Harris Mar 13 '12 at 21:35
    
Worked! Thank you! –  Stephen Mar 13 '12 at 23:25

Well, for one thing, you're modifying your query inside of your Loop, which begins with your if ( have_posts() ) : conditional. Move this outside the query:

 <?php query_posts( 'category_name=category&showposts=2'); ?>

Second, you don't want to use query_posts() for anything other than modifying the Primary Loop. As this is a custom Page template, the Primary Loop is the Page content. Your category query is a secondary loop. So, you should be using WP_Query() instead of query_posts() for this secondary loop.

Third, whenever you create a secondary loop that requires pagination, you need to tell WordPress to paginate based on the secondary loop's query, instead of paginating based on the Primary Loop's query. That question has been dealt with extensively here on WPSE; here's one example.

To modify it for your use case, do something like this:

<?php
// Custom query args
$category_query_args = array(
    'category_name' => 'category'
    'posts_per_page' => 2
);
// Custom query
$category_query = new WP_Query( $category_query_args );

// Reset pagination
global $wp_query;
$temp = $wp_query;
$wp_query= null;
$wp_query = $category_query;

// Output the Loop
if ( $category_query->have_posts() ) : 
?>
<div id="container">
    <div id="content" role="main">

while ( $category_query->have_posts() ) : $category_query->the_post();
?>
<div>
     <div>
        <div class="thumb">
            <?php the_post_thumbnail('thumbnail'); ?> 
        </div>
        <h2 class="entry-title" style="text-align:center;"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
        <div class="the-excerpt">
             <?php echo content(15, __('(Read More)')); /* twentyten_posted_on(); */ ?>
        </div><!-- .entry-meta -->
    </div>
</div>
<?php
endwhile;

// Custom navigation
?>
<br clear="all" />
<?php if (function_exists('wp_corenavi')) wp_corenavi(); ?>

    </div><!-- #content -->
</div><!-- #container -->

<?php
endif;

// Reset query
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>

Edit

p.s. you should be using posts_per_page rather than showposts, which is deprecated.

Edit 2

By the way, WordPress already provides a function for paginating links: paginate_links(). It is pretty straightforward to use, though it does require more arguments than the typical WordPress function. Here's how I use it in Oenology.

share|improve this answer
    
I'm getting the same issue after I duplicated this. Here is my code: pastebin.com/eRbJPAqy –  Stephen Mar 13 '12 at 20:40
    
And what happens if you use core navigation functions, rather than the custom navigation function? –  Chip Bennett Mar 13 '12 at 22:41
    
Using the wordpress default nav I'm getting the same issue, pastebin.com/UfCzLTfY –  Stephen Mar 13 '12 at 23:24

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.