Max Lynn Max Lynn - 1 year ago 72
Javascript Question

Wordpress category post AJAX Pagination

I'm really struggling to find a way to create pagination with ajax for my Wordpress posts. The solutions that I have found do not work.

To be more informative about this here is a link that has bullets at the bottom for pagination. Once these are clicked I want the effect of the site to load the new posts without triggering a page refresh.

My question is, is there any good tutorials out there that you may have had success with for this type of effect.

Let me know if you need more information.

****** UPDATE ******

function kriesi_pagination($pages = '', $range = 2) {
$showitems = ($range * 2)+1;

global $paged;
if (empty($paged)) $paged = 1;

if ($pages == '') {
global $wp_query;
$pages = $wp_query->max_num_pages;
if (!$pages) {
$pages = 1;

if (1 != $pages) {
echo "<div class='pagination'><div class='pagination-container'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a>";

for ($i=1; $i <= $pages; $i++) {
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";

if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>&rsaquo;</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
echo "</div>\n</div>\n";


This is my PHP that I'm using, how would I use this php to create an ajax request so that the page doesn't reload?


What you need to do is to prevent default on the pagination links, and send an AJAX request to get the posts. Wordpress works in this way for AJAX: you send all your requests to wp-admin/admin-ajax.php with an action parameter that will identify the request in order to catch it in functions.php using wp_ajax_nopriv_my_action and wp_ajax_my_action hooks.

So basically you will do this in your template file:

<script type="text/javascript">
    jQuery(document).ready(function($) {
        $('.pagination a').click(function(e) {
            e.preventDefault(); // don't trigger page reload
            if($(this).hasClass('active')) {
                return; // don't do anything if click on current page
                '<?php echo admin_url('admin-ajax.php'); ?>', // get admin-ajax.php url
                    action: 'ajax_pagination',
                    page: parseInt($(this).attr('data-page')), // get page number for "data-page" attribute
                    posts_per_page: <?php echo get_option('posts_per_page'); ?>
                function(data) {
                    $('#content-posts').html(data); // replace posts with new one

You'll have to change classes names / attributes etc depending of your template.

And on the functions.php side:

function my_ajax_navigation() {
    $requested_page = intval($_POST['page']);
    $posts_per_page = intval($_POST['posts_per_page']) - 1;
    $posts = get_posts(array(
        'posts_per_page' => $posts_per_page,
        'offset' => $page * $posts_per_page
    foreach ($posts as $post) {
        setup_postdata( $post );
        // good thing to do would be to include your post template
add_action( 'wp_ajax_ajax_pagination', 'my_ajax_navigation' );
add_action( 'wp_ajax_nopriv_ajax_paginationr', 'my_ajax_navigation' );

The thing is to query the posts of the page requested (so we calculate the offset from the page number and the posts per page option), and display them with the template you use for single posts.

You may want to manipulate the browser history too, for that you should check on the History API.