无限滚动追加下一个产品类别术语页

时间:2020-09-10 作者:Juárez

我正在使用无限卷轴和以下伟大的代码from here 在术语循环中生成下一个分类术语链接。当您到达当前类别的末尾时,我想添加下一个Woocommerce产品类别。

一切正常,只是只加载了一个术语,然后它说;没有其他要显示的项目;。甚至浏览器url也会根据滚动定位的术语而变化。你知道这样做是否可行吗?

下一个/上一个术语链接代码:

    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 );
}
无限滚动代码

$(\'#main\').infiniteScroll({
      path: ".tax-pages a.next",
    hideNav: ".tax-pages",
    append:  "#main ul.products",
    status: \'.page-load-status\'
});

1 个回复
最合适的回答,由SO网友:Juárez 整理而成

我发现无限卷轴有一个updateurl函数:

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

相关推荐

在同一类别中加载更多帖子-AJAX+木材

我在WordPress中使用木材。我正在尝试创建一个带有"Load more posts" 按钮当用户点击按钮时,我想显示10篇同类文章,并加载10篇同类文章"Load more posts"当类别之间没有区别时,一切都很好。按钮"Load more posts" 工作正常。显示10个立柱。但是当我点击按钮试图显示同一类别的帖子时"Load more posts". 不显示post。类别有什么问题?能帮我个忙吗?存档。php$