WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] previous_posts_link not showing on page 2 while next_posts_link is. (5 posts)

  1. Nerina-01
    Member
    Posted 11 months ago #

    I have a Static page set as front_page and I use a WP_Query the page template includes to display posts. I have a template part product-list.php and in it I have the following:

    $paged = (get_query_var('page')) ? get_query_var('page') : 1;
    $query = new WP_Query( array( 'paged' => $paged ) );
    	$products_args = array (
    		'post_type' => 'bb_product',
    		'posts_per_page' => 3,
    		'paged' => $paged
    	);

    And then after the $products_query loop:

    <?php if ( $products_query->max_num_pages > 1 ) : ?>
        <div class="navigation">
            <?php
            // Bring $wp_query into the scope of the function
            global $wp_query;
            // Backup the original property value
            $backup_page_total = $wp_query->max_num_pages;
            // Copy the custom query property to the $wp_query object
            $wp_query->max_num_pages = $products_query->max_num_pages;
            ?>
            <div class="alignright"><?php next_posts_link('Next Entries'); ?></div>
            <div class="alignleft"><?php previous_posts_link('Previous Entries'); ?></div>
            <?php
            // Finally restore the $wp_query property to it's original value
            $wp_query->max_num_pages = $backup_page_total;
            ?>
        </div>
    <?php endif; ?>

    The problem is that when I am on page 1, I can see the next_posts_link, but when I am on page 2 there is still only the next_posts_link and no previous_posts_link (the HTML markup is there, but the link is missing). Even when I'm on the last page, there is only the next_posts_link (although there are no next posts) and no previous_posts_link.

    I've read many threads with similar/same problems, but I can't solve it. This post helped me to show the navigation at all, before both links where missing.

    My test site is here, to see it scroll down, the pagination has a red background.

    Can somebody help or point me in the right direction?

    Thank you.

  2. Nerina-01
    Member
    Posted 11 months ago #

    Just an error in the first sentence, it should say:

    I have a Static page set as front_page and I use a WP_Query to display posts.

  3. bcworkz
    Member
    Posted 11 months ago #

    You are basically trying to fake the posts_link functions into thinking your query is the main query. You need to do more than copy over the max_num_pages value. I can't condone trying to fool functions, but you could try copying your entire query object to the global, after backing it up of course. No guarantees that will work.

    The correct approach is to either make your query the main query via the 'pre_get_posts' action or to handle your own pagination the same way the posts_link functions do. It's mainly a matter of calculating the proper OFFSET parameters for the next query based on page number and posts per page.

  4. Nerina-01
    Member
    Posted 11 months ago #

    Thank you for answering bcworkz,

    That gives me some insight, I didn't understand pagination, nor did I know what I was doing. I'm on the road for one week, but will get to the code as soon as possible.

  5. Nerina-01
    Member
    Posted 11 months ago #

    I could not solve the problem with pre_get_posts. But with the paginate_links function it`s working properly now:

    $paged = (get_query_var('page')) ? get_query_var('page') : 1;
    $products_args = array (
    	'post_type' => 'bb_product',
    	'posts_per_page' => 2,
    	'paged' => $paged
    );

    Then my WP_Query and then the pagination:

    if ( $products_query->max_num_pages > 1 ) :
    	$big = 999999999; // need an unlikely integer
    	echo paginate_links( array(
    		'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    		'format' => '?paged=%#%',
    		'current' => $paged,
    		'total' => $products_query->max_num_pages
    	) );
    endif;

    Thank you for your help bcworkz.

Reply

You must log in to post.

About this Topic