I’m using Infinite Scroll and the following great code from here to generate Next Taxonomy Term Link in a term loop. I want to ajax append Next Woocommerce Product Category when you reach the end of the current category.
Everything works fine except it only loads 1 term and then it says “No more Items to show”. Even the browser url changes depending on which term you are scroll positioned. Any idea if this is possible this way?
Next / Prev Terms links code:
function get_tax_navigation( $taxonomy = 'category', $direction = '' )
{
// Make sure we are on a taxonomy term/category/tag archive page, if not, bail
if ( 'category' === $taxonomy ) {
if ( !is_category() )
return false;
} elseif ( 'post_tag' === $taxonomy ) {
if ( !is_tag() )
return false;
} else {
if ( !is_tax( $taxonomy ) )
return false;
}
// Make sure the taxonomy is valid and sanitize the taxonomy
if ( 'category' !== $taxonomy
|| 'post_tag' !== $taxonomy
) {
$taxonomy = filter_var( $taxonomy, FILTER_SANITIZE_STRING );
if ( !$taxonomy )
return false;
if ( !taxonomy_exists( $taxonomy ) )
return false;
}
// Get the current term object
$current_term = get_term( $GLOBALS['wp_the_query']->get_queried_object() );
// Get all the terms ordered by slug
$terms = get_terms( $taxonomy, ['orderby' => 'slug'] );
// Make sure we have terms before we continue
if ( !$terms )
return false;
// Because empty terms stuffs around with array keys, lets reset them
$terms = array_values( $terms );
// Lets get all the term id's from the array of term objects
$term_ids = wp_list_pluck( $terms, 'term_id' );
/**
* We now need to locate the position of the current term amongs the $term_ids array.
* This way, we can now know which terms are adjacent to the current one
*/
$current_term_position = array_search( $current_term->term_id, $term_ids );
// Set default variables to hold the next and previous terms
$previous_term = '';
$next_term = '';
// Get the previous term
if ( 'previous' === $direction
|| !$direction
) {
if ( 0 === $current_term_position ) {
$previous_term = $terms[intval( count( $term_ids ) - 1 )];
} else {
$previous_term = $terms[$current_term_position - 1];
}
}
// Get the next term
if ( 'next' === $direction
|| !$direction
) {
if ( intval( count( $term_ids ) - 1 ) === $current_term_position ) {
$next_term = $terms[0];
} else {
$next_term = $terms[$current_term_position + 1];
}
}
$link = [];
// Build the links
if ( $previous_term )
$link[] = '<div class="tax-pages"> <a class="prev" href="' . esc_url( get_term_link( $previous_term ) ) . '">' . $previous_term->name . '</a></div>';
if ( $next_term )
$link[] = '<div class="tax-pages"> <a class="next" href="' . esc_url( get_term_link( $next_term ) ) . '">' . $next_term->name . '</a></div>';
return implode( ' ...|... ', $link );
}
Infinite Scroll code
$('#main').infiniteScroll({
path: ".tax-pages a.next",
hideNav: ".tax-pages",
append: "#main ul.products",
status: '.page-load-status'
});
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I discovered Infinite Scroll has an updateurl function:
var nextURL;
function updateNextURL( doc ) {
nextURL = $( doc ).find('.tax-pages a.next').attr('href');
}
// get initial nextURL
updateNextURL( document );
// init Infinite Scroll
var $container = $('#main').infiniteScroll({
// use function to set custom URLs
path: function() {
return nextURL;
},
hideNav: ".tax-pages",
append: "#main ul.products",
status: '.page-load-status',
debug: true
});
// update nextURL on page load
$container.on( 'load.infiniteScroll', function( event, response ) {
updateNextURL( response );
});
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0