Offset and Pagination with a Custom Query
-
I’ve been trying to get “offset” to work with pagination. Using this page from the www.ads-software.com Docs, I’ve tried to get offset to work with my posts query. For whatever reason, the code doesn’t do anything except to make my pagination links (Page 1 of 2, 1, 2, 3, 4, >, etc.) disappear.
I’ve got a custom post type that I’m querying, and want to leave out just the first post because I am going to style just the first post differently from the rest of the posts that are listed. Here’s what I’ve got so far:
<!-- begin WordPress' code --> <?php add_action('pre_get_posts', 'query_offset', 1 ); function query_offset(&$query) { //Before anything else, make sure this is the right query... if ( ! $query->is_posts_page ) { return; } //First, define your desired offset... $offset = 1; //Next, determine how many posts per page you want (we'll use WordPress's settings) $ppp = get_option('posts_per_page'); //Next, detect and handle pagination... if ( $query->is_paged ) { //Manually determine page query offset (offset + current page (minus one) x posts per page) $page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp ); //Apply adjust page offset $query->set('offset', $page_offset ); } else { //This is the first page. Just use the offset... $query->set('offset',$offset); } } add_filter('found_posts', 'adjust_offset_pagination', 1, 2 ); function adjust_offset_pagination($found_posts, $query) { //Define our offset again... $offset = 1; //Ensure we're modifying the right query object... if ( $query->is_posts_page ) { //Reduce WordPress's found_posts count by the offset... return $found_posts - $offset; } } ?> <!-- end WordPress' code --> <!-- begin my code --> <?php query_posts(array( 'post_type' => 'post_type_name', 'posts_per_page' => 8, 'orderby'=> 'date', 'order' => 'DESC', 'paged' => $paged )); ?> <?php if(have_posts()) : while(have_posts()) : the_post(); ?> <?php the_title(); ?> <?php endwhile; ?> <?php wp_pagenavi(); ?> <?php endif; wp_reset_query(); ?> <!-- end my code -->
Any ideas how to work WP’s code with my query? Thanks in advance!
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Offset and Pagination with a Custom Query’ is closed to new replies.