WordPress.org

Ready to get started?Download WordPress

Forums

query_posts pagination not working (7 posts)

  1. redconservatory
    Member
    Posted 3 years ago #

    I am not getting pagination to work with the following code, can anyone advise?

    <?php
      $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $args = array(
           'cat' => '5',
           'post_type' => 'post',
           'posts_per_page' => 6,
           'paged' => $page,
           );
    
     query_posts($args);
    
     while (have_posts()) { the_post();
    
    ?>
     <div class="project_item">
            <div class="dotted">
              <hr />
            </div>
            <div class="project_thumb"><a href="<?php echo get_permalink(); ?>"><img src="<?php getCustomField('news_thumbnail'); ?>"  /></a></div>
            <div class="project_entry"><h4><a href="<?php echo get_permalink(); ?>"><?php getCustomField('news_title'); ?></a></h4>
              <?php getCustomField('news_excerpt'); ?>
              <a href="<?php echo get_permalink(); ?>" class="readmore">Read more..</a> </div>
          </div>
    
          <?php
    
    }
    
    wp_reset_query();  // Restore global post data
    
    ?>
  2. SimonJ
    Member
    Posted 3 years ago #

    You have a mistake in your code :

    last line in your $args array :

    'paged' => $page,

    you should use :

    'paged' => $paged,

    S.

  3. redconservatory
    Member
    Posted 3 years ago #

    I made that change and that doesn't seem to help - any ideas?

    I thought
    'paged' => $page,

    was referencing the variable $page from this line below?

    $page = (get_query_var('paged')) ? get_query_var('paged') : 1;

  4. SimonJ
    Member
    Posted 3 years ago #

    Oh well...! Forgive me! :-))) I took the code from my own site, i tought you used the one in the codex. :-)))

    The only difference between the code I use and yours, is that you use the while loop in {}... With no endwhile...

    Try with this version :

    <?php while (have_posts()) : the_post(); ?>
    
    --- Your stuff ---
    
    <?php endwhile; ?>

    This is the only thing I can see offhands.

    S.

  5. redconservatory
    Member
    Posted 3 years ago #

    Hm I did try this but no luck either. Do you might - I mean, if you could - posting the code that works from your site? Maybe I'm missing something...

    Thanks so much for your help on this!

  6. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    http://go.rvoodoo.com/paginate

    is my example....

    <?php query_posts( array(
          'posts_per_page' => 5,
          'cat' => '10',
          'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
     ));
    ?>
  7. redconservatory
    Member
    Posted 3 years ago #

    Okay, thanks, all of this works now.
    I just needed to add "previous" and "next" links after the loop (duh):

    <?php
    	 $args = array(
    				   'cat' => '5',
    				   'post_type' => 'post',
    				   'posts_per_page' => 6,
    				   'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
    				   );
    
    	query_posts($args);
    
    while (have_posts()) : the_post();
     /* Do whatever you want to do for every page... */
    ?>
    	<div class="project_item">
            <div class="dotted">
              <hr />
            </div>
            <div class="project_thumb"><a href="<?php echo get_permalink(); ?>"><img src="<?php getCustomField('news_thumbnail'); ?>"  /></a></div>
            <div class="project_entry"><h4><a href="<?php echo get_permalink(); ?>"><?php getCustomField('news_title'); ?></a></h4>
              <?php getCustomField('news_excerpt'); ?>
              <a href="<?php echo get_permalink(); ?>" class="readmore">Read more..</a> </div>
          </div>
    
          <?php
    
    endwhile;
    ?><div class="navigation">
      <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
      <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
    </div>
    <?php
    wp_reset_query();  // Restore global post data
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic