1

OK, I am developing a plugin which uses a custom page template. The way the plugin works, it reads a list of items to be displayed from a custom database table, then displays the list. So far, that works fine.

However, I would like to add pagination to this list. Here's an extract from the template:

<div class="container">
    <div id="liquid" role="main">
        <h2>Our Inventory</h2>
        <?php
            // Pull list of vehicles from database
            $sql = "SELECT `Stock Number`, `VIN`, `Year`, `Make`, `Model`
                FROM $inv_table
                ORDER BY `Stock Number` ASC";
            $vehicles = $wpdb->get_results ( $sql, ARRAY_A );
        ?>

        <?php
            global $post;
            $page = untrailingslashit ( get_permalink ( $post->ID ) );
        ?>

        <!-- display the rows -->
        <?php foreach ( $vehicles as $vehicle ) : ?>
            <div>
                 <a href="<?php echo $page. '/' . $vehicle ['VIN']; ?>" >
                     <?php echo $vehicle['Stock Number']; ?>
                 </a>
                 <p>
                    Stock: <?php echo $vehicle ['Stock Number']; ?><br />
                    VIN: <?php echo $vehicle ['VIN']; ?><br />
                    Mileage: <?php echo $vehicle ['Mileage']); ?><br />
                 </p>
            </div>
            <div class="clearfix"></div>
        <?php endforeach; ?>
    </div>
</div>

What I want to do now is paginate with a page break every, say, 20 items.

I've looked around a lot but I can't find anything that does exactly what I'd like it to do...

Any help will be greatly appreciated!

  • 1
    Straight PHP - no need for any WP functions here. Get a count of your results with count( $vehicles ) and using an increment var loop through them a page at a time. – jdm2112 Sep 20 '16 at 19:32
  • 1
    Possible duplicate? wordpress.stackexchange.com/questions/53194/… that link have answer with real code sample on paginating wpdb results – Ahmed Fouad Sep 20 '16 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.