Infinite-scroll: custom query
-
Hi,
I’m a theme developer. For the theme I’m currently working on I require Infinite Scroll to work with a custom loop in certain cases.
Background:
The theme’s guidelines predicate consistency in design regardless of the page that is being viewed. Each page has 2 relevant components:- the article : this will be a post, page etc.
- the aside : this is a list of other articles
Constraints:
- The main wordpress loop may return 0 articles (ie. a
is_404() || !have_posts()
) - The main wordpress loop may return 1 article only (ie.
is_singular()
) - The main wordpress loop may return multiple articles
Implications of the design and constraints
Covering the third case above is no issue. Infinite Scroll is configured as per the below and works on any page where the main wordpress loop returns multiple articles.Covering the first two cases is where I would need some support. In order to respect the design I’m firing off a custom loop. This loop pulls in the most recent articles. I would need Infinite Scroll to work with this loop to maintain consistency with the other pages (ie.
is_home() || is_archive() || is_search()
… ).I’m a little lost in the documentation of IS and not sure where to turn at this stage. I know the render option won’t do any good. It doesn’t set the query IS works with, it merely employs it as far as I can tell. I’m thinking I think I’ll need to use the
'infinite_scroll_query_object'
filter but am a little hesitant. Where would I set it? And more importantly where and when would I reset it to the main loop? Any guidance here would be appreciatedMy JetPack config looks like:
function big_cookbook_jetpack_setup() { add_theme_support('infinite-scroll', array( 'container' => 'blog-list"', 'render' => 'big_cookbook_infinite_scroll_render', 'footer' => false, 'type' => 'click', 'wrapper' => false, )); } add_action('after_setup_theme', 'big_cookbook_jetpack_setup'); function big_cookbook_infinite_scroll_render() { while (have_posts()) { the_post(); get_template_part('template-parts/content-thumb', get_post_format()); } }
My custom loop (‘content-recent.php’) looks like this :
$default_posts_per_page = get_option( 'posts_per_page' ); $r = new WP_Query( apply_filters( 'widget_posts_args', array( 'posts_per_page' => $default_posts_per_page, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true ) ) ); if ($r->have_posts()) : while ( $r->have_posts() ) : $r->the_post(); get_template_part('template-parts/content-thumb', get_post_format()); endwhile; wp_reset_postdata(); endif;
- The topic ‘Infinite-scroll: custom query’ is closed to new replies.