WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Pagination not showing in custom loop (15 posts)

  1. Peter vanDoorn
    Member
    Posted 2 years ago #

    First off, I have done extensive research on this issue and have followed everything that seems pertinent, but I still can't get pagination to display on an index page when called inside a custom loop, so I'm turning to you for help!

    Here's my code (edited down so that it doesn't include any non-essential bits):

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;  $blog = new WP_Query();
    $args = array( 'post_type' => 'blog', 'posts_per_page' => 3, 'paged' => $paged );
    $blog->query($args);
    if ( $blog->have_posts() ) :
    while($blog->have_posts()) : $blog->the_post();
    	// Content
    endwhile;
    posts_nav_link();
    endif;

    Please help, I'm losing what little hair I have left!

    Thanks in advance.

    Peter

  2. Peter, I've moved this post to Hacks, please do not post duplicate threads.

    I'm guessing this is for a front page template? Via pastebin.com can you post the whole file (really, please use pastebin.com, don't paste it here) and share the link to that paste?

    It may make it easier to illustrate what's going on and why.

  3. keesiemeijer
    moderator
    Posted 2 years ago #

    Is this the main loop on your home page?

    If so, try it without the new WP_Query, a normal loop and this in your theme's functions.php:

    function my_post_queries( $query ) {
    
      // not an admin page and it is the main (paginated) query
      if (!is_admin() && $query->is_main_query()){
    
        if(is_home()){
          $query->set('post_type', array('blog'));
          $query->set('posts_per_page', 3);
        }
    
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );
  4. Peter vanDoorn
    Member
    Posted 2 years ago #

    Hi.

    Yes, I didn't want to cross-post, but if you post in the wrong forum, there's no way to move it to the correct one. Perhaps that is a feature that might be useful for a future forum update?

    As for the offending loop, no - it is a custom loop that queries a custom post type (I've used the normal post type for something else, so had to use a custom post type for this). The rest of the template file is literally just get_header() before it and get_footer() after.

    Thanks for your help

    Peter

  5. keesiemeijer
    moderator
    Posted 2 years ago #

    Have you tried it with a normal loop like this and the other code in your theme's functions.php:

    <?php
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    	// Content
    endwhile;
    posts_nav_link();
    endif;
    ?>

    The code in your functions.php sets the post type to "blog" on your home page
    You can only have one loop paginated, usually the main posts loop.

    Is this for your home page?
    Or is this a static front page?

  6. Peter vanDoorn
    Member
    Posted 2 years ago #

    No, this is not the home page. It's a custom loop on another page.

    Thanks

  7. Peter vanDoorn
    Member
    Posted 2 years ago #

    Oh, and it's the only loop on the page too.

  8. keesiemeijer
    moderator
    Posted 2 years ago #

    Ah, then my solution doesn't work.
    So this is on a custom page template
    Try it with a query_posts:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array( 'post_type' => 'blog', 'posts_per_page' => 3, 'paged' => $paged );
    query_posts($args);
    if ( have_posts() ) :
    while(have_posts()) : the_post();
    	// Content
    endwhile;
    posts_nav_link();
    endif; ?>
  9. Peter vanDoorn
    Member
    Posted 2 years ago #

    Hi again

    Yes, it is on a custom page template, and - yes - it does make a difference, in that it brings up the pagination links. However... clicking on one (to go to blog/page/2/) just brings up a 404 error.

    When I was researching this problem, everything I read said that using query_posts() causes the most problems with pagination (such as 404 errors with pretty permalinks) and that you should always do a direct wp_query() call.

    So, colour me flummoxed!

    Thanks again

  10. keesiemeijer
    moderator
    Posted 2 years ago #

    Can you provide a link to the page in question. I have this working on my site.

  11. Peter vanDoorn
    Member
    Posted 2 years ago #

    Unfortunately, the site is not yet live, so I can't :-(

    I know this is an issue that many other people have had before, but most seem to have solved it by using the $paged thing. Can you see anything wrong with the theory of my original code?

    Thanks for taking the trouble - much appreciated!

  12. keesiemeijer
    moderator
    Posted 2 years ago #

    Can you see anything wrong with the theory of my original code?

    No, but page templates are not meant to be paginated. That's why we need to do some workarounds on them to get pagination working. Could you try it with this:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array( 'post_type' => 'blog', 'posts_per_page' => 3, 'paged' => $paged );
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query($args);
    if ( $wp_query->have_posts() ) :
    while ($wp_query->have_posts()) : $wp_query->the_post();
    	// Content
    endwhile;
    posts_nav_link();
    endif;
    $wp_query = null; $wp_query = $temp;
    ?>

    This also works for me.
    And also re-save your permalinks under wp-admin > Settings > Permalinks.

  13. Peter vanDoorn
    Member
    Posted 2 years ago #

    Hi.

    Again, your latest code does generate the pagination, but the links still give 404 errors.

    I have also tried resetting the permalinks, but no luck (just to clarify - I'm using /%category%/%postname%/). Of course, it does work with default, ugly permalinks, but that's not an option.

    I think I'm just going to have to give up, set posts_per_page to -1 and hope that the client doesn't blog too much ;-)

    Thanks for trying!

  14. keesiemeijer
    moderator
    Posted 2 years ago #

    The only thing I can think of is trying it with the default theme twenty eleven and with all plugins de-activated.

    Also, why did you call this an index page in your first post? This is a "Page" that shows posts right?

  15. Peter vanDoorn
    Member
    Posted 2 years ago #

    Hi keesimeijer

    Sorry for the confusion with calling it an index page (although, technically, a single page in a pretty permalinked WP site is an index page since it is served up as /pageslug/index.htm)!

    Anyway, I've solved it! It just goes to show sometimes that you can't see for looking - my custom post type had the same name as the page slug, so it fell foul of that nasty WP 404 glitch! I knew that! :-)

    So, to anyone else reading this thread, the moral is: WordPress doesn't like it if you have a custom post type with the same name as a page slug!

    Thanks for all your help.

    Peter

Topic Closed

This topic has been closed to new replies.

About this Topic