0

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'
});
0

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 );
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.