0

There are a lot of similar questions asked here, but none of the suggestions seems to work for me.

I have the following code on my home page:

get_template_part( 'category-clients' );

And category-clients.php looks like this:

        <div id="content">
        <?php
          global $paged;
          global $wp_query;
          $temp = $wp_query; 
          $wp_query = null; 
          $wp_query = new WP_Query(); 
          $wp_query->query('cat=251&posts_per_page=3'.'&paged='.$paged);
          while ($wp_query->have_posts()) : $wp_query->the_post(); 
        ?>

        <?php the_title(); ?>

        <?php endwhile; ?>

        <div id="pagination">
        <?php next_posts_link('&laquo; Older Entries') ?>
        <?php previous_posts_link('Newer Entries &raquo;') ?>
        </div>

        <?php 
          $wp_query = null; 
          $wp_query = $temp; 
        ?>

    </div><!-- #content -->

Also... below this code, on category-clients.php, I have a jQuery script for AJAX loading of the different pages:

    <script>
jQuery(document).ready(function(){
// AJAX pagination
jQuery(function(jQuery) {
    jQuery('#content').on('click', '#pagination a', function(e){
        e.preventDefault();
        var link = jQuery(this).attr('href');
        jQuery('#content').fadeOut(500, function(){
            jQuery(this).load(link + ' #content', function() {
                jQuery(this).fadeIn(500);
            });
        });
    });
});
});
</script>

Now... if I go to $myurl/clients, the pagination works just fine, but on the homepage it doesn't. Any ideas? Thank you!

1 Answer 1

2

I finally made it work. Leaving the answer here in case someone else have the same problem.

        <div id="content">

        <?php
            global $paged;
            global $new_query;
            $paged = get_query_var('page');
            query_posts( array( 'cat' => 251, 'paged' => $paged ) );
            $new_query = new WP_Query();
            $new_query->query('cat=251&posts_per_page=3&paged='.$paged);
        ?>

        <?php while ($new_query->have_posts()) : $new_query->the_post(); ?>
        <?php the_title(); ?>

        <?php endwhile; ?>

        <div id="pagination">
        <?php next_posts_link('&laquo; Older Entries', $new_query->max_num_pages) ?>
        <?php previous_posts_link('Newer Entries &raquo;') ?>
        </div>

    </div><!-- #content -->
2
  • it's probably helpful to explain why this solved it. On page post type the page number is in the page global rather than paged, which is for archive pagination.
    – Milo
    Jan 7, 2015 at 21:34
  • This answer also helped me.
    – Lemon Kazi
    Aug 22, 2016 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.