Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save isotrope/1930f90518bdbfa074c3 to your computer and use it in GitHub Desktop.
Save isotrope/1930f90518bdbfa074c3 to your computer and use it in GitHub Desktop.
<?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', 'pla_extra_js_settings' );
function pla_extra_js_settings( $js_settings ) {
$js_settings['text'] = __( "Plus de contenus", "pla" );
$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', 'pla_allow_extra_vars', 11 );
function pla_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 pla_infinite_scroll_query_args_adjust( $query_args ) {
if ( array_key_exists( 'extra', $query_args ) ) {
$extra = $query_args['extra'];
if ( $extra['is_admin'] === 'true') {
return $query_args;
}
if ( $extra['is_home'] === 'true' || $extra['is_front_page'] === 'true' ) {
$query_args['posts_per_page'] = 13;
$query_args['post__not_in'] = pla_get_home_header_post_ids();
} else {
$query_args['posts_per_page'] = 12;
}
}
return $query_args;
}
add_filter( 'infinite_scroll_query_args', 'pla_infinite_scroll_query_args_adjust', 11 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment