Support » Fixing WordPress » Trouble with next_posts_link() on page of posts

  • Resolved knottulf


    I am pretty new to WordPress, and have a difficult time with creating navigation links on a page of posts.

    This seems to work, except that the next and previous links only show the same two posts – although the page number in the url changes. When changing the showposts=2 to 8, all the 8 posts are shown in one page.

    $query_string = "'cat=5,3&posts_per_page=2&paged='.$paged.'&orderby=date&order=asc'";
    $posts = query_posts( $query_string );
      if( $posts ) : ?>
    	<div class="post">
    		<h1>Ordered by Post Title (Ascending)</h1>
    		<?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
    			<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
    			<p><?php the_content(); ?></p>
    		<?php endforeach; ?>
    <?php endif; ?>
    <div class="navigation">
          <?php // $paged = $paged + 1; ?>
          <div class="alignleft"><?php next_posts_link('« Older articles') ?></div>
    <div class="alignright"><?php previous_posts_link('Newer articles »') ?></div>

    I don’t understand the life cycle of $paged–and sure neither a lot some other necessary stuff here.

    Any help appreciated!

Viewing 8 replies - 1 through 8 (of 8 total)
  • esmi


    Forum Moderator

    You have to set the $paged variable:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $query_string = "'cat=5,3&posts_per_page=2&paged='.$paged.'&orderby=date&order=asc'";

    Thank you! But this doesn’t seem to make any difference.
    Could you please explain the expression?

    // Set the $paged variable
    $paged =
      // Conditional check (if there is a paged query var)
      // Set to that value
      ? get_query_var('paged')
      // Else set to one
      : 1;

    It’s called a ternary comparison, and it’s shorthand for doing.

    if( get_query_var('paged') ) {
      $paged = get_query_var('paged');
    else {
      $paged = 1;

    In regard to your problem, might be worth checking if you’re getting a value for the paged var..

    echo '<h2>The paged query variable is set to "' . get_query_var('paged') . '"</h2>';

    The code above should be placed before the $paged line.

    Thank you Mark

    So now I understand, and learnt some php at the same time!
    I checked your last addition, and the value paged is received correctly, and so is the assignment of the variable. So the counting of the logical total number of pages is also correct. However, still this still does not affect which posts are shown, which are the posts belonging to the first page–on all the pages.

    Sorry I don’t get this right, seems kind of silly 🙁

    If this is your posts page, why are you running a custom loop, the regular loop should be fine (you’ve assigned it the posts page, it will naturally output a regular list of posts), are you attaching a page template to this given page? And if so, i assume the code above is in that template, correct?

    This would be easier with a simple filter and it seems you’re only setting a couple of query parameters, please clarify on the above and i’ll explain further… 🙂

    It is not my regular posts listing, but rather a page set up specifically for listing most of the posts from several categories in various ways, with a separate template. These posts are also shown using the ordinary posts templates.
    I could easily believe that I approach this task from a difficult angle, and that I’m building code that maybe wasn’t meant for this use. But I would also very much like to be able to set up templates for queries and listings for various uses.

    Moderator keesiemeijer



    try it with this:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $query_string = 'cat=5,3&posts_per_page=2&paged='.$paged.'&orderby=date&order=asc';

    HEY, thats it!!
    The whole problem was only the extra quote signs in the $query_string assigment 😀

    So easy, yet so difficult. Thanx very much to all of you for helping me out on this!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Trouble with next_posts_link() on page of posts’ is closed to new replies.