WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Change posts_per_page count on paginated pages (16 posts)

  1. cjhmdm
    Member
    Posted 2 years ago #

    Hello,

    I'm trying to set up a custom paginated (!important) page that will show 9 posts on page 1, and 18 posts on each subsequent paginated page.

    Here's an example of the query I'm using:

    // pagination for custom page(s)
    $paged = (get_query_var('page')) ? get_query_var('page') : 1;
    
    //get posts_per_page count
    if ( $paged < 2 ) : $postcount = 9;
    else: $postcount = 18;
    endif;
    
    //query args
    $idObj1 = get_category_by_slug('updates');
    $catid1 = $idObj1->term_id;
    $args1 = array(
    	'posts_per_page'        => $postcount,
        'order'                 => 'DESC',
        'post_status'           => 'publish',
        'cat'                   => $catid1,
        'paged'                 => $paged
    );
    
    //$updates = get_posts($args1);
    query_posts($args1);

    The problem that I am running in to is that no posts are showing up on the paginated pages. 1 thing to note: Pagination works perfectly fine if I remove if statement.

    I'm assuming it's due to my changing the value of posts_per_page while processing the same loop, but I'm not sure about how to go about doing what I need.

    Any ideas?

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    Is this on a custom Page template or on category.php?

    Try it with this: http://pastebin.com/04yXKjzC

  3. cjhmdm
    Member
    Posted 2 years ago #

    It's for a custom page. Sorry about that, I should have specified. Checking out your example now and will post back with results.

    Thanks

  4. cjhmdm
    Member
    Posted 2 years ago #

    @keesiemeijer:

    This sort of works. Posts now show up on pages 2-x, but once I view a paginated page, the pagination itself is removed.. for example, if I click on page 2, I see the proper posts, but I no longer have links back to page 1 or to pages 3-5. This is using wp_pagenavi btw. I also get the same result when using the standard next/previous style pagination.

    Any ideas?

  5. keesiemeijer
    moderator
    Posted 2 years ago #

    Am I correct in thinking you want this only on the "updates" category pages?

    Try it with a new WP_query loop: http://codex.wordpress.org/Function_Reference/WP_Query#Usage

  6. cjhmdm
    Member
    Posted 2 years ago #

    No sir, you are incorrect.

    This is a custom page (main_page.php) and basically serves as the home page. This page will only show posts from the 'updates' category, while other pages - which will be similar to this page - will show posts from other categories.

    I won't be using a 'categories' type page at all.

  7. cjhmdm
    Member
    Posted 2 years ago #

    If it helps, look at it as a CMS as opposed to a blog.

    Nonetheless, I may have figured out what was going wrong with the example you gave, and am testing it out now.

    Thanks again

  8. keesiemeijer
    moderator
    Posted 2 years ago #

    If it's a static front page you have to set the $paged variable like so:

    // pagination for custom page(s)
    if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
    elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
    else { $paged = 1; }
  9. cjhmdm
    Member
    Posted 2 years ago #

    Thanks a lot for the help keesiemeijer. Everything is working as I wanted it to. The only caveat to doing this is that page 1 shows there are a total of 5 pages, but page 2 shows there are only 3 pages total, same with page 3. Obviously, this is due to showing more posts on pages 2 & 3 than on page 1, so it's something I can live with.

    Thanks again keesiemeijer and have a good one :)

  10. keesiemeijer
    moderator
    Posted 2 years ago #

    Ah yes, I didn't think about that. Back to the drawing board. I'm glad you got it (somewhat) resolved.

  11. cjhmdm
    Member
    Posted 2 years ago #

    Well, I almost have it how I want it... I'm trying to use paginate_links instead of wp-pagenavi and now all I need to do is find out how to remove the link to the last page next to the 'Next' link and I'll be set... I'll probably have to create my own function for it based off the original one but oh well lol...

  12. cjhmdm
    Member
    Posted 2 years ago #

    Just to update with the solution I settled on...

    I used the following method to handle pagination:

    global $wp_query;
    
    $big = 999999999; // need an unlikely integer
    if ($wp_query->max_num_pages > 1 && $paged < 2): $totalpages = 2;
    else: $totalpages = $wp_query->max_num_pages;
    endif;
    
    echo paginate_links( array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var('paged') ),
            'total' => $totalpages
    ) );

    The reason for doing it this way is - as explained above - changing the posts_per_page variable from pge 1 to page 2-x causes page 1 to shoe more page links than exist... for example, while on page 1, it shows there are a total of 5 pages but when you go to page 2, there are only a total of 3. Which then leads to a blank page if a visitor were to click on the link to page 5, et al.

    The above basically does the same thing but in reverse.

    First the script checks that there is more than 1 page, as well as checks whether or not you're on page 1. If both return true then it only shows 2 pages. Then, once you go to page 2, you get the rest of the pagination functioning properly.

    Thanks again keesiemeijer

  13. keesiemeijer
    moderator
    Posted 2 years ago #

    I changed it so wp-pagenavi gets a fake query (only on the first page) to show the correct number of pages. The correct number is calculated and given to the query (used by pagenavi) on all pages. Here is an example with the loop: http://pastebin.com/rB3epkL0

  14. cjhmdm
    Member
    Posted 2 years ago #

    Works perfectly keesiemeijer.

    Thank you so much for this :)

  15. keesiemeijer
    moderator
    Posted 2 years ago #

    You're welcome. Now I'm glad you got it totally resolved!

  16. keesiemeijer
    moderator
    Posted 2 years ago #

    I changed it a bit so there is no need for the extra fake query for wp-pagenavi: http://pastebin.com/wEu5zRKT

Topic Closed

This topic has been closed to new replies.

About this Topic