WordPress custom query pagination and Ajax

I have some category pages and single pages on my wordpress website with a custom query. The pagination, which works on the homepage, is based on a script which loads the posts on the same page.

The problem is that pagination works in single pages and categories only with the <?php previous_posts_link('&laquo; Previous') ?>
The script does not work on the single pages and categories.

Read More

Here’s the code:

<div class="main_container">
<div id="load_posts_container">
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
   $args=array(
      'post_type'=>'post',
      'cat' => '-28' . $category_ID,
      'posts_per_page' => 8,
      'paged'=>$paged
   );
   $temp = $wp_query;
   $wp_query= null;
   $wp_query = new WP_Query($args);

   if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();   ?>
<!--ARTICLES MARKUP HERE-->
<?php   endwhile; endif; 
   ?>
<!--//load_posts_container-->
<
<div class="load_more_cont">
   <p>Altri eventi <br/>
      <span class="load_more_text"><?php next_posts_link('<img src="' . get_bloginfo('stylesheet_directory') . '/images/load-more-image.png" alt="Altri Eventi a Roma"/>', $wp_query->max_num_pages) ?></span>
   </p>
</div>
<!--//load_more_cont-->
<?php $wp_query = null;
   $wp_query = $temp;
   wp_reset_query(); ?>
<script type="text/javascript">
   // Ajax-fetching "Load more posts"
   $('.load_more_cont a').live('click', function(e) {
    e.preventDefault();
    //$(this).addClass('loading').text('Loading...');
           //$('.load_more_text a').html('Loading...');
    $.ajax({
        type: "GET",
        url: $(this).attr('href') + '#main_container',
        dataType: "html",
        success: function(out) {
            result = $(out).find('#load_posts_container .home_post_box');
            nextlink = $(out).find('.load_more_cont a').attr('href');
                           //alert(nextlink);
            //$('#boxes').append(result).masonry('appended', result);
                       $('#load_posts_container').append(result);
            //$('.fetch a').removeClass('loading').text('Load more posts');
                           //$('.load_more_text a').html('Load More');


            if (nextlink != undefined) {
                $('.load_more_cont a').attr('href', nextlink);
            } else {
                $('.load_more_cont').remove();
                                   $('#load_posts_container').append('<div class="clear"></div>');
                                 //  $('.load_more_cont').css('visibilty','hidden');
            }

                       if (nextlink != undefined) {
                           $.get(nextlink, function(data) {
                             //alert(nextlink);
                             if($(data + ":contains('home_post_box')") != '') {
                               //alert('not found');
                                 //                      $('.load_more_cont').remove();
                                                       $('#load_posts_container').append('<div class="clear"></div>');        
                             }
                           });                        
                       }

        }
    });
   });
</script>

Any idea?
Thanks

Related posts

Leave a Reply

1 comment

  1. If WordPress isn’t parsing the paged query var for those pages (which I have not verified), you might want to skip get_query_var('paged'), and just use a plain old fashion $_GET['paged']