Filtering
Filter and reset an Ajax Load More instance
In this example:
A custom menu will filter Ajax Load More between recent work and recent articles.
By utilizing Ajax Load More public and callback JavaScript functions you can create an intuitive filtering engine that adjusts shortcode parameters with the click of a button.
🔥 We’ve recently released the Filters add-on that makes filtering with Ajax Load More easier than ever!
-
Ajax Load More 7.0
Refactoring Ajax Load More for the future.
-
Tracking Pageviews with Google Analytics 4
Measuring page views automatically with GA4 and Ajax Load More.
-
Ajax Load More 5.6
A release focused on new features, facet filtering and extending existing functionality.
-
Instant Images Adds Openverse API Integration
Instant Images now offers API integrations with Unsplash, Openverse, Pixabay and Pexels, providing access to millions of high-quality images...
HTML
Filtering is achieved by passing updated shortcode parameters as data attributes to the ajaxloadmore.filter
public JavaScript function.
<div class="alm-filter-nav">
<button data-repeater="template_8" data-post-type="portfolio" data-posts-per-page="3" data-scroll="false" data-button-label="More Work">Recent Work</button>
<button data-repeater="default" data-post-type="post" data-posts-per-page="5" data-scroll="true" data-button-label="More Articles">Recent Articles</button>
</div>
HTMLNote: Shortcode parameters with underscores must be converted to dashes while using data attribute binding. e.g. post_type
will be data-post-type
.
JavaScript
The following code snippet should copied and placed inside your website JavaScript file.
/**
* Filter button click event.
*/
function filterClick(event) {
event.preventDefault();
// Exit if button active.
if (this.classList.contains('active')) {
return;
}
// Get .active element.
var activeEl = document.querySelector('.alm-filter-nav button.active');
if (activeEl) {
activeEl.classList.remove('active');
}
// Add active class.
this.classList.add('active');
// Set filter params
var transition = 'fade';
var speed = 250;
var data = this.dataset;
// Call core Ajax Load More `filter` function.
// @see https://connekthq.local/plugins/ajax-load-more/docs/public-functions/#filter
ajaxloadmore.filter(transition, speed, data);
}
// Get all filter buttons.
var filter_buttons = document.querySelectorAll('.alm-filter-nav button');
if (filter_buttons) {
// Set initial active item.
filter_buttons[0].classList.add('active');
// Loop buttons.
[].forEach.call(filter_buttons, function (button) {
// Add button click event.
button.addEventListener('click', filterClick);
});
}
JavaScriptThe following shortcode was used to create the Filtering example.
[ajax_load_more post_type="portfolio" scroll="false" posts_per_page="3" button_label="Load More Work"]
The following Repeater Template was used to create the Filtering example.