WordPress.org

Ready to get started?Download WordPress

Forums

Why is pagination not working? (7 posts)

  1. Adam
    Member
    Posted 1 year ago #

    Can some one tell me why on this piece of code the pagination doe not work?

    by "does not work" I mean that if you go to the previous page, the posts on the page before are the exact same. No matter how far back you go they remain the same. Its like the query doesn't update.

    The query in this case being used is:

    $query = array('posts_per_page' => 20);

  2. Sridhar Katakam
    Member
    Posted 1 year ago #

    Try changing it to

    $query = array('posts_per_page' => 20, 'paged' => $paged);

  3. Adam
    Member
    Posted 1 year ago #

    I did with no change in result. I have seen that trick before and alas it is not working. Any other ideas?

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Maybe this will work:

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $wp_query = new WP_Query(array('posts_per_page' => 20, 'paged' => $paged));
    ?>

    On what theme template file are you doing this. There are more ways you can query the loop:
    http://www.billerickson.net/customize-the-wordpress-query/
    http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/
    http://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts

  5. Sridhar Katakam
    Member
    Posted 1 year ago #

    Also a recent article I came across covers this.

    #5 at http://kovshenin.com/2013/better-wordpress-theme-development/

  6. Adam
    Member
    Posted 1 year ago #

    @keesiemeijer again I have already tried that, as stated in the post above your reply. This is just being used in index.php, nothing fancy.

    Again the query is:

    @query = array('posts_per_page' => 20);

    I chose this query to test out pagination. See me as an example. Click "previous" and "next" to see that the posts are all the same.

    This would either indicate the query based on this code is not working as expected or is not resetting it's self properly.

    srikat this hasn't really helped me, thanks. I am developing a web based framework for theme development, so most of what you wrote is already covered.

    Any ideas? I have tried wp_reset_query(); and the trick I am doing now. I have tried the "$paged trick."

  7. keesiemeijer
    moderator
    Posted 1 year ago #

    If this is on your front page you could try with this in your functions.php:

    function my_post_queries( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
        if(is_home()){
          $query->set('posts_per_page', 20);
        }
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    Remove the query from your index.php and use a normal loop:

    if(have_posts()){
    while(have_posts()){
    the_post();
    $this->_title($this->_options);
    the_excerpt();
    }
    next_posts_link('« Older Entries');
    previous_posts_link('Newer Entries »');
    }

Topic Closed

This topic has been closed to new replies.

About this Topic