Support » Fixing WordPress » Paginate HTML table with Posts Per Page

  • Basically, I use the following query to retrieve a list of movie reviews:

    ‘<?php $loop = new WP_Query( array( ‘post_type’ => ‘movie-reviews’, ‘posts_per_page’ => 10 ) ); ?>’

    I then populate an HTML table with the data retrieved. Here’s my question. Obviously, I can change the number of reviews displayed in the table by simply changing the posts_per_page number.

    But how do I get multiple pages to show up? So, for example, if I have 30 reviews then I want something at the bottom of my table that shows different page numbers that can be clicked on.

    Thanks in advance for any and all help!

Viewing 7 replies - 1 through 7 (of 7 total)
  • You will need to determine the page number, add it to the query, and provide code to let the viewer select the page.

    Here is sample code to determine the page number and add it to the query:

    $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
    $loop = new WP_Query( array( 'post_type' => 'movie-reviews', 'posts_per_page' => 10, 'paged' => $paged ) );

    The WP PageNavi plugin provides a nicer version of the viewer’s selections than the WP default previous/next_posts_link() function.

    I too am looking for help with this.

    vtxyzzy, when you say that you need to “provide code to let the viewer select the page”, what are you meaning? Do we have to manually add the link that is clicked?

    The default WP pagination code uses the previous_posts_link() and next_posts_link() functions to usually show ‘Older Posts’ and ‘Newer Posts’.

    The WP PageNavi plugin gives a nicer version of pagination, but for either one, your template must include the proper code. WP PageNavi has instructions for using it in your template.

    What if I am not doing this in a template? I am trying to create a custom query on a specific page in order to format the list of posts from a particular category in a specific manner. But, I still want to be able to use the post_per_page => 10 and have the older/newer links. Any suggestions?

    You use the same code whether in a template or some other source.

    I don’t think I am going about this right. My php knowledge is limited, and nothing seems to be working the way I want.

    This is what I have. It gets the posts correctly and displays them right, but I cannot get the page links to appear.

    $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
    $args = array( 'cat' => 42, 'order' => 'ASC', 'paged' => $paged, 'posts_per_page => 10');
    $custom_query = new WP_Query( $args );
    while($custom_query->have_posts()) : $custom_query->the_post(); ?>
    	<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
    		<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>

    Can you see what I am doing wrong?

    You don’t have any code to show the links. You need to insert code for paginate_links() before you reset the postdata.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Paginate HTML table with Posts Per Page’ is closed to new replies.