Guest User

Untitled

a guest
Apr 1st, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Includes a few useful predicates in the bootstrapped `infiniteScroll` JS object
  5. * that is served along with the rest of the document upon initial page request.
  6. */
  7. add_filter( 'infinite_scroll_js_settings', 'mcsf_extra_js_settings' );
  8. function mcsf_extra_js_settings( $js_settings ) {
  9. $js_settings['query_args']['extra'] = array(
  10. 'is_home' => is_home(),
  11. 'is_front_page' => is_front_page(),
  12. 'is_admin' => is_admin()
  13. );
  14. return $js_settings;
  15. }
  16.  
  17. /**
  18. * Whitelists the `extra` variable that holds the predicates, so that IS doesn't ignore
  19. * it when reconstructing the original query using the query args from the IS AJAX
  20. * request.
  21. *
  22. * Filter priority is not actually necessary.
  23. */
  24. add_filter( 'infinite_scroll_allowed_vars', 'mcsf_allow_extra_vars', 11 );
  25. function mcsf_allow_extra_vars( $allowed_vars ) {
  26. $allowed_vars[] = 'extra';
  27. return $allowed_vars;
  28. }
  29.  
  30. /**
  31. * Now that we've made these predicates available, let's use them do make our adjustments
  32. * to $query_args.
  33. *
  34. * Filter priority is indeed important, as we need to run this after `inject_query_args`.
  35. */
  36. function mcsf_infinite_scroll_query_args_adjust( $query_args ) {
  37. if ( array_key_exists( 'extra', $query_args ) ) {
  38. $extra = $query_args['extra'];
  39.  
  40. if ( $extra['is_admin'] ) {
  41. return $query_args;
  42. }
  43.  
  44. if ( $extra['is_home'] || $extra['is_front_page'] ) {
  45. $query_args['posts_per_page'] = 13;
  46. } else {
  47. $query_args['posts_per_page'] = 9;
  48. }
  49. }
  50.  
  51. return $query_args;
  52. }
  53. add_filter( 'infinite_scroll_query_args', 'mcsf_infinite_scroll_query_args_adjust', 11 );
Advertisement
Add Comment
Please, Sign In to add comment