Wordpress Pagination Class

Fri, Sep 28, 2007

PHP, WordPress

I wrote a simple, yet extremely useful Wordpress pagination class. The class is not complete yet, and I work on it as I move along and need new features or actions. So you have the freedom to take the class and customize/expand it as you might need to. First off, you can download the class below :

Wordpress Pagination Class

In order to use the class, you’ll need to include the class file. With the class file included, you can initialize the class and then execute the startPaging() class method. Before you execute the method, you need to specify some variable values. See the example below :

include_once(ABSPATH . ‘wp-content/plugins/yourplugin/class.pagination.php’);
$tableName = $wpdb -> prefix . ‘yourplugintable’;
$tableFields = ‘`id`, `title`, `description`’;
$pagination = new Pagination($tableName, $tableFields);
$pagination -> perPage = 10;
$pagination -> pluginUrl = ‘?page=yourplugin.php&’;
$allRecords = $pagination -> startPaging($_GET[‘wpMailinglistPage’]);

From there, you can simply loop through your records with a foreach(); or something similar to output the records. In order to output the page numbers/links and the “previous”/”next” links, you can echo the pagination(); class method. See the example below :

<?= $pagination -> pagination(); ?>

Good luck! I hope this is useful!
Please feel free to post your updated source code, ideas and comments.

, , ,

This post was written by:

Antonie Potgieter - who has written 46 posts on Lost-In-Code.

I (Antonie Potgieter) am a software engineer/web developer located in South Africa. My full-time work is the management of Tribulant Software and the development of its software packages.

Contact the author

Leave a Reply