Infinite Scroll – works for default post type/query, how to use w/ custom query?
-
Hi,
My website has two primary post “types”, regular text blog posts (“posts”) and a custom post type “Lessons” (which uses a custom taxonomy).
Infinte-Scroll works perfect for the regular posts. I just added the following to my functions file, and they were good to go:
/** * Add lazy-loading infinite scroll support */ if( !function_exists('init_infinite_scroll') ) : function init_infinite_scroll() { add_theme_support( 'infinite-scroll', array( 'type' => 'scroll', 'container' => 'content', 'posts_per_page' => 5, 'footer_widgets' => false, 'footer' => false )); } add_action( 'after_setup_theme', 'init_infinite_scroll' ); endif;
However, infinite-scroll does nothing for my custom post type. Below is the code on my
page-lessons.php
, which is where I need infinite-scroll to work:<ul id="content" class="small-block-grid-1 medium-block-grid-2 large-block-grid-4"> <?php do_action('foundationPress_before_content'); ?> <?php /* if URL query value is a valid category, get all lessons for that category. If it is not a valid category (or not category is provided), get ALL lessons. */ $validCats = ['study-skills', 'time-management', 'math', 'tutoring', 'reading', 'online-learning']; // get specific category from query string, else get all categories if ( isset($_GET['lc']) && in_array($_GET['lc'], $validCats) ) { $terms = $_GET['lc']; } else { $terms = $validCats; } $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; $args = [ 'post-type' => 'lesson', 'post-status' => 'publish', 'posts_per_page' => 4, 'paged' => $paged, 'tax_query' => [ [ 'taxonomy' => 'subjects', 'field' => 'slug', 'terms' => $terms ] ] ]; ?> <?php $lessons = new WP_Query($args); ?> <?php if( $lessons->have_posts() ) : ?> <?php while($lessons->have_posts()) : $lessons->the_post(); ?> <?php get_template_part( 'content', 'lesson'); ?> <?php endwhile; ?> <?php else : ?> <?php get_template_part('content', 'none') ?> <?php do_action('foundationPress_before_pagination') ?> <?php endif; ?> </ul> <?php previous_posts_link( "prev" , $lessons->max_num_pages ); ?> <?php next_posts_link( "next" , $lessons->max_num_pages ); ?>
The
next
andprev
links work as expected, and paginate me through my content. However, infinite-scroll does not seem to initialize (does nothing).How can I get infinite-scroll working with the above code?
- The topic ‘Infinite Scroll – works for default post type/query, how to use w/ custom query?’ is closed to new replies.