WordPress.org

Ready to get started?Download WordPress

Forums

pagination problem with pages (2 posts)

  1. jimmy72
    Member
    Posted 1 year ago #

    Hi, I have following chunk of code in my "product-category" page template. It shows all the products in a specific category (Id=92). But I limited the number of products to be shown per page to "2".
    So if I had 10 products there would be 5 pages. But when the loop is finished it only shows the first page with 2 products.
    So how can I add next en previous links to have access to those other 4 pages?

    Thanks

    Jimmy

    <?php

    $args = array(
    'posts_per_page' => 2,
    'post_type' => 'page',
    'post_parent' => 92,
    );

    $query= new WP_Query($args);

    while ( $query->have_posts() ) : $query->the_post();
    the_title();
    endwhile; wp_reset_postdata();

    ?>

  2. vtxyzzy
    Member
    Posted 1 year ago #

    Change your query arguments to this:

    $posts_per_page = 2;
    $current = ($current = get_query_var('paged')) ? $current : 1;
    $args = array(
       'posts_per_page' => $posts_per_page,
       'post_type' => 'page',
       'paged' => $current,
       'post_parent' => 92,
    );

    Then, between the endwhile; and wp_reset_postdata(), insert this:

    global $wp_rewrite;
    
    $pagination_args = array(
     'base' => @add_query_arg('paged','%#%'),
     'format' => '',
     'total' => $query->max_num_pages,
     'current' => $current,
     'show_all' => false,
     'type' => 'plain',
    );
    
    if( !empty($wp_query->query_vars['s']) )
     $pagination_args['add_args'] = array('s'=>get_query_var('s'));
    
    echo paginate_links($pagination_args);

Topic Closed

This topic has been closed to new replies.

About this Topic