August 26, 201213 yr Basically, I'm trying to use paginate_links to get paging on my posts, and after lots of Google searches, I've ended up with the code below: <?php // Get standard page content while ( have_posts() ) : the_post(); the_content(); endwhile; // Display 5 posts from "whats on" category $my_query = new WP_Query('category_id=5'); while ($my_query->have_posts()) : $my_query->the_post(); ?> <div class="whats_on_item clearfix"> <?php $do_not_duplicate = $post->ID; ?> <a href="<?php the_permalink(); ?>"><img src="<?php the_field( 'thumbnail' ) ?>" /></a> <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4> <em><?php the_date(); ?></em><br> <?php echo get_the_excerpt(); ?> </div> <?php endwhile; ?> <div class="post_paging"> <?php $big = 999999999; // need an unlikely integer echo paginate_links( array( 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $my_query->max_num_pages ) ); ?> The above displays the pagination absolutely fine, however it is showing the same posts on each page (kinda defeating the point of pagination!). Searching the net, I have tried several suggestions, however I am a relative newbie to Wordpress and PHP so I could really use some help here. Any help much appreciated!
August 26, 201213 yr Author SOLVED Had to modify WP_Query like so: $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $my_query = new WP_Query('category_id=5&paged=' . $paged); Working code as follows: // Display 5 posts from "whats on" category $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $my_query = new WP_Query('category_id=5&paged=' . $paged); while ($my_query->have_posts()) : $my_query->the_post(); ?> <div class="whats_on_item clearfix"> <?php $do_not_duplicate = $post->ID; ?> <a href="<?php the_permalink(); ?>"><img src="<?php the_field( 'thumbnail' ) ?>" /></a> <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4> <em><?php the_date(); ?></em><br> <?php echo get_the_excerpt(); ?> </div> <?php endwhile; ?> <div class="post_paging"> <?php $big = 999999999; // need an unlikely integer echo paginate_links( array( 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $my_query->max_num_pages ) ); ?> Amazing what a good nights sleep does :-)
August 26, 201213 yr Author Another issue now - can't get pagination to work on the search.php page. Copied pretty much the same code as above: <?php while ( have_posts() ) : the_post(); ?> <div class="whats_on_item clearfix"> <?php $do_not_duplicate = $post->ID; ?> <a href="<?php the_permalink(); ?>"><img src="<?php the_field( 'thumbnail' ) ?>" /></a> <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4> <div class="date"><?php the_date(); ?></div> <?php echo get_the_excerpt(); ?> </div> <?php endwhile; ?> <?php if ( $my_query->max_num_pages > 1 ) : ?> <div class="post_paging"> <?php $big = 999999999; // need an unlikely integer echo paginate_links( array( 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $my_query->max_num_pages ) ); ?> </div> <?php endif; ?> The pagination doesn't show at all. If someone thinks they can help, I'll happily PM you a link to the site. Any help appreciated
August 26, 201213 yr Need to make sure the search query is paged again. Try this in functions.php (off the top of my head so not tested, but should work): add_action('pre_get_posts', 'rd_paginate_query'); function rd_paginate_query($query = false) { if (!is_a($query, 'WP_Query') || !$query->is_main_query()) { return; } $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $query->set('paged', $paged); } Edited August 26, 201213 yr by Renaissance-Design
Create an account or sign in to comment