Infinity scroll repeating initial posts
-
I am trying to use infinity scroll and for some reason it is repeating the initial set of posts and when I reach the end it is jumping back up and repeating the same thing again, causing an erratic scrolling behaviour. I have a custom query which is ordering the posts by a field (added by ACF) called
event_date.I am adding infinity scroll support to my theme as follows:
function infinite_scroll_init() { add_theme_support( 'infinite-scroll', array( 'container' => 'events', 'footer' => false, 'footer_widgets' => false, 'posts_per_page' => get_option('posts_per_page'), 'render' => 'infinite_scroll_render', ) ); } add_action( 'after_setup_theme', 'infinite_scroll_init'); function infinite_scroll_render() { get_template_part( 'content','posts' ); } function jetpack_infinite_scroll_query_args( $args ) { $args['order'] = 'DESC'; $args['meta_key'] = 'event_date'; $args['orderby'] = 'meta_value'; return $args; } add_filter( 'infinite_scroll_query_args', 'jetpack_infinite_scroll_query_args' );My
content-posts.phplooks like this (it is also being included fromindex.phpwithget_template_part().$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; $posts_per_page = (isset($posts_per_page)) ? $posts_per_page : get_option('posts_per_page'); $args = array( 'post_type' => 'post', 'meta_key' => 'event_date', 'orderby' => 'meta_value date', 'order' => 'DESC', 'posts_per_page' => $posts_per_page, 'paged' => $paged ); $date_format = "jS F Y"; $query = new WP_Query( $args ); if ($query->have_posts()) : ?> <?php $index = 0; ?> <?php while ( $query->have_posts() ) : $query->the_post(); ?> <?php $event_date = get_field('event_date'); if ($event_date) { $event_date = date_i18n($date_format, strtotime($event_date)); } else { //fallback which shouldn't really happen, event_date is compulsory $event_date = get_the_date($date_format); } ?> <?php $thumbnail_url = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'large' )[0]; ?> <a class="news" style="background:url(<?php echo $thumbnail_url; ?>) no-repeat center center;" href="<?php the_permalink(); ?>"> <span class="caption"><h1><?php the_title(); ?></h1><h2>- <?php echo $event_date; ?> -</h2></span> </a> <?php if ($index % 3 == 3) : ?></div><?php endif; ?> <?php $index++; ?> <?php endwhile; // end of the loop. ?> <?php if ($index % 3 != 3) : ?></div><?php endif; ?> <?php endif; ?> <?php wp_reset_query(); ?>It seems like it has something to do with the
$pagedvariable, but can’t figure how to go around this. What can I do to get this to work properly?
The topic ‘Infinity scroll repeating initial posts’ is closed to new replies.