0

I have the following custom query and im attempting to paginate the links of previous archive posts using the example from the wordpress codex

http://codex.wordpress.org/Function_Reference/paginate_links#Basic_Example

Am i inserting this in the wrong place? I inserted before the end of the query with the basic arguments, and there is just a place space at the moment.

Any advice / tutorial is appreciated.

Update 2: Ive now tried the change in the wp_query in the pagination piece, and still no luck.

<div id = "innerpagemaincontent">
<h2 class="innerpagetitle">Latest News</h2>
    <div class="r-post clearfix">

 <?php // custom loop query
$carouselPosts = new WP_Query('showposts=5');

// Pagination fix
$original_query = $wp_query;
$wp_query = NULL;
$wp_query = $carouselPosts;

// open loop
if ( $carouselPosts ->have_posts() ) : while ( $carouselPosts->have_posts() ) :  $carouselPosts->the_post(); ?>

<div class="otherrecentpostswrap">
   <div id="postdetails"><div class="boldyfont">Posted by <?php the_author(); ?><br/><?php the_category(','); ?></div><a href="<?php the_permalink(); ?>"><?php the_date (); ?></a></div>
    <div class="thumb"><a href="<?php the_permalink(); ?>">
        <?php the_post_thumbnail(array(160,160)); ?><span class="overlay">    </span></a></div>

        <div id="postewrapper">
         <div class="recentpostscontent">
        <a href="<?php the_permalink(); ?>">
        <span class="innerpageposttitle"><?php the_title(); ?></a></span>

    <?php the_excerpt(); ?>
    <br/>
    <br/>
    <a class="readmorelink" href="<?php the_permalink(); ?>"> Read More </a>  </div></div>
<br/><br/>


</div>
<?php endwhile; ?>
<?php endif; ?>


<?php paginate_links(  array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $carouselPosts->max_num_pages
    ) ); ?>

<?php wp_reset_query(); ?>
3
  • P.S. - Pagination Not Working With Custom Loop does apply. That question and its answer are not specific to the WP PageNavi Plugin. The issue is the way that WordPress handles pagination when using a secondary loop. If the problem is that WordPress isn't recognizing that the loop is paginated, then the answer to that question will be a part of your ultimate solution here. Oct 25, 2013 at 13:47
  • I tried the above and it didn't work thou :( Oct 26, 2013 at 7:53
  • possible duplicate of How to fix pagination for custom loops? Oct 31, 2013 at 13:43

2 Answers 2

0

One issue at least: you'll want to close the loop before outputting your pagination links, or else they'll be output on every iteration of the loop. But you've got paginate_links() before endwhile;.

Try this arrangement instead:

// open loop
if ( have_posts() ) : while ( have_posts() ) : the_post();

    // Loop markup

// close loop
endwhile; endif;

// pagination here
paginate_links( $args );

For a custom query loop, you'll want to handle the query accordingly:

// custom loop query
$custom = new WP_Query( $args );

// Pagination fix
$original_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom;

// open loop
if ( $custom->have_posts() ) : while ( $custom->have_posts() ) : $custom->the_post();

    // Loop markup

// close loop
endwhile; endif;

// pagination here
paginate_links( $args );

// reset query
wp_reset_query();
15
  • do i still need to add anything extra though chip to the functions? As obviously i went via the basic example on the codex Oct 25, 2013 at 12:39
  • What kind of extra things? I think the first step is to make sure that your links are working properly; making paginate_links() work is its own beast. :) Oct 25, 2013 at 12:41
  • so would it be best for me to just move the pagination after the end while... or will it still not work :S Oct 25, 2013 at 12:45
  • Yeah, you want to add pagination outside the loop, but inside of your query manipulation/reset. Oct 25, 2013 at 12:48
  • Hi Chip, please see update, would that work with my rewrite. I dont wanna do the php too open, i would rather do it closed per section so i can get my head round it Oct 25, 2013 at 12:52
0

I copied your code and tried at my localhost. It was not working as $wp_query->max_num_pages is 0 but when i changed to 'total' => $carouselPosts->max_num_pages it showing paging. I hope it will work in your system also. if it doesn't work, let me know, i will try to help again

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.