Forums › Forums › Search & Filter Pro › double load effect when there is only one page of results
Tagged: infinite scroll
- This topic has 9 replies, 4 voices, and was last updated 2 years, 11 months ago by Trevor.
-
Anonymous(Private) February 20, 2020 at 6:52 pm #234506
Hi,
I’m having trouble preventing the double load effect when there is only one page of results. When there are multiple pages, the span with the data attribute works well. Example: https://abe.stpaulmedia.net/ But when there is only one page, the spinner appears if you scroll. Example: https://abe.stpaulmedia.net/?_sft_focus=civics-citizenship
The display results method is custom, and I’m using the template engine Twig via the plugin Timber. Here are the relevant bits:
functions.php
function spm_modify_blog_homepage_query( $query ) { if ( ! is_admin() && $query->is_home() && $query->is_main_query() ) { $sf_id = get_field( 'sf_id', 'option' ); $query->set( 'orderby', 'title' ); $query->set( 'order', 'asc' ); $query->set( 'search_filter_id', $sf_id ); } } add_action( 'pre_get_posts', 'spm_modify_blog_homepage_query' );
index.php
$context = Timber::context(); $context['catalog_search_form'] = Timber::get_widgets( 'catalog-search-form' ); $context['posts'] = new Timber\PostQuery(); $context['pagination'] = Timber::get_pagination(); $templates = array( 'index.twig' ); Timber::render( $templates, $context );
index.twig
<div class="ajax-container"> <div class="is-container"> {% if posts %} {% for post in posts %} {% include 'blocks/preview.twig' %} {% endfor %} {% endif %} {% if pagination.current == pagination.total %} <p data-search-filter-action="infinite-scroll-end">End of results</p> {% endif %} </div> </div>
I tried searching the forums but didn’t find this specific issue. Thanks for taking a look, and let me know if you need more info.
Ross Moderator(Private) February 27, 2020 at 7:25 pm #235131This reply has been marked as private.Ross Moderator(Private) February 28, 2020 at 7:00 pm #235238Hi Jonathan
So I found a solution to this.
S&F was ignoring
data-search-filter-action='infinite-scroll-end'
on page load, and only looking for it AFTER fetching more results via Ajax.I have updated our plugin to fix this.
All you need to do is add the attribute to your results somewhere, when you know the current set of results are in fact the total set of results, for my testing, I modified our infinite scroll template by doing this:
Found <?php echo $query->found_posts; ?> Results<br /> <?php //prevent double load effect if the first set of results are the total results $page = intval($query->query_vars['paged']); $infinite_scroll_end_markup = ''; if($page==1){ if(intval($query->found_posts) <= intval($query->query_vars['posts_per_page'])){ //then all the results loaded in the first page $infinite_scroll_end_markup = " data-search-filter-action='infinite-scroll-end'"; } } ?> <div class='search-filter-results-list' <?php echo $infinite_scroll_end_markup; ?>>
…
I hope that helps 🙂
Ross Moderator(Private) March 3, 2020 at 12:24 pm #235408Hi Jonathan
Just to confirm that all worked ok and I can close this thread?
Thanks
-
AuthorPosts