• Resolved zuperm4n

    (@zuperm4n)


    I’m using a child-theme of devdm’s Bootstrap template and I’ve created a static page template which contains a loop to display all posts from a parent category called Portfolio.

    This parent category has sub-categories, and using the WP_Query below, I am able to display as many posts as I wish per page with no issues.

    I’m trying to add Pagination now, and I’ve followed every tutorial I can find but for some reason it won’t work. All I want is a simple ‘More’ link at the bottom of my loop using
    next_posts_link( 'More' )

    My code is as follows:

    <?php $paged = ( get_query_var('page') ) ? get_query_var('page') : 1; ?>
    <?php $category = get_post_meta($post->ID, 'portfolio category', true); ?>
    <?php $query = new WP_Query( array( 'posts_per_page' => 3, 'cat' => $category, 'paged' => $paged )); ?>
    <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
    
    // Stuff that prints post details (works)
    
    <?php endif; ?>
    <?php endwhile; ?>
    
    <?php next_posts_link( 'More' ); ?>
    
    wp_reset_postdata();
     else : ?>
     <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
     <?php endif; ?>

    The code is really broken-down, I know. The pagination does nothing at all here. I’ve inspected the page source, and nothing is printed at all.

    Just to be clear, this is on a custom page template. i.e., the header of this file says:
    Template Name: Portfolio List

    I’d appreciate any help!

Viewing 1 replies (of 1 total)
  • Thread Starter zuperm4n

    (@zuperm4n)

    Okay, I’ve solved my issue (so soon!?!?).

    I’ll leave this post here in case someone else has this issue.

    The WordPress Codex says:

    Add the $max_pages parameter to the next_posts_link() function when querying the loop with WP_Query

    I did initially do this by replacing:
    <?php next_posts_link( 'More' ); ?>
    with
    <?php echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages );
    from an online tutorial but I forgot to change ‘$the_query‘ to ‘$query‘.

    So to confirm, if you are using WP_Query to query your loop, you need to use <?php echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages ); for pagination.

Viewing 1 replies (of 1 total)
  • The topic ‘Yet another pagination issue… I can't see why it won't work!’ is closed to new replies.