Last active
May 5, 2020 11:37
-
-
Save mcsf/6c5cda9ee61acc8849c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Includes a few useful predicates in the bootstrapped `infiniteScroll` JS object | |
* that is served along with the rest of the document upon initial page request. | |
*/ | |
add_filter( 'infinite_scroll_js_settings', 'mcsf_extra_js_settings' ); | |
function mcsf_extra_js_settings( $js_settings ) { | |
$js_settings['query_args']['extra'] = array( | |
'is_home' => is_home(), | |
'is_front_page' => is_front_page(), | |
'is_admin' => is_admin() | |
); | |
return $js_settings; | |
} | |
/** | |
* Whitelists the `extra` variable that holds the predicates, so that IS doesn't ignore | |
* it when reconstructing the original query using the query args from the IS AJAX | |
* request. | |
* | |
* Filter priority is not actually necessary. | |
*/ | |
add_filter( 'infinite_scroll_allowed_vars', 'mcsf_allow_extra_vars', 11 ); | |
function mcsf_allow_extra_vars( $allowed_vars ) { | |
$allowed_vars[] = 'extra'; | |
return $allowed_vars; | |
} | |
/** | |
* Now that we've made these predicates available, let's use them do make our adjustments | |
* to $query_args. | |
* | |
* Filter priority is indeed important, as we need to run this after `inject_query_args`. | |
*/ | |
function mcsf_infinite_scroll_query_args_adjust( $query_args ) { | |
if ( array_key_exists( 'extra', $query_args ) ) { | |
$extra = $query_args['extra']; | |
if ( $extra['is_admin'] ) { | |
return $query_args; | |
} | |
if ( $extra['is_home'] || $extra['is_front_page'] ) { | |
$query_args['posts_per_page'] = 13; | |
} else { | |
$query_args['posts_per_page'] = 9; | |
} | |
} | |
return $query_args; | |
} | |
add_filter( 'infinite_scroll_query_args', 'mcsf_infinite_scroll_query_args_adjust', 11 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment