srikat

Untitled

Aug 17th, 2016
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. add_action( 'pre_get_posts', 'myprefix_query_offset', 1 );
  2. function myprefix_query_offset( &$query ) {
  3.  
  4. // Before anything else, make sure this is the right query...
  5. if ( ! $query->is_category() ) {
  6. return;
  7. }
  8.  
  9. // First, define your desired offset...
  10. $offset = 1;
  11.  
  12. // Next, determine how many posts per page you want (we'll use WordPress's settings)
  13. $ppp = get_option( 'posts_per_page' );
  14.  
  15. // Next, detect and handle pagination...
  16. if ( $query->is_paged ) {
  17.  
  18. //Manually determine page query offset (offset + current page (minus one) x posts per page)
  19. $page_offset = $offset + ( ( $query->query_vars['paged']-1 ) * $ppp );
  20.  
  21. // Apply adjust page offset
  22. $query->set( 'offset', $page_offset );
  23.  
  24. }
  25. else {
  26.  
  27. // This is the first page. Just use the offset...
  28. $query->set( 'offset', $offset );
  29.  
  30. }
  31. }
  32.  
  33. add_filter( 'found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
  34. function myprefix_adjust_offset_pagination( $found_posts, $query ) {
  35.  
  36. // Define our offset again...
  37. $offset = 1;
  38.  
  39. // Ensure we're modifying the right query object...
  40. if ( $query->is_category() ) {
  41. // Reduce WordPress's found_posts count by the offset...
  42. return $found_posts - $offset;
  43. }
  44. return $found_posts;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment