我使用以下操作將帖子加載到wordpress網站的索引頁面。問題在於何時到達最後一頁,並且沒有更多帖子需要加載。它只是不斷重新加載最後一頁。當不再存在時停止加載帖子
任何想法可以阻止這種情況的發生?謝謝。
var $content = '#content';
var $nav_wrap = '.navigation';
var $anchor = '.navigation a.next';
var $text = 'Load More';
var $next_href = $($anchor).attr('href'); // Get URL for the next set of posts
$($nav_wrap).html('<a id="almc-load-more" href="' + $next_href + '">' + $text + '</a>');
$('#almc-load-more').click(function(e) {
e.preventDefault();
$.get($(this).attr('href'), '', function(data) {
var $timestamp = new Date().getTime();
var $new_content = $($content, data).wrapInner('<div class="almc-loaded" id="almc-' + $timestamp + '" />').html(); // Grab just the content
$next_href = $($anchor, data).attr('href'); // Get the new href
$('html,body').animate({scrollTop: $($nav_wrap).position().top}, 'slow'); // Animate scroll
$($nav_wrap).before($new_content); // Append the new content
$('#almc-' + $timestamp).hide().fadeIn('slow'); // Animate load
$('#almc-load-more').attr('href', $next_href); // Change the next URL
$('.almc-loaded ' + $nav_wrap).remove(); // Remove the original navigation
});
});
以上代碼從這裏取:http://kaspars.net/blog/wordpress/jquery-script-for-loading-more-posts
將所有鏈接存儲在數組中 – mplungjan
傳遞服務器返回的數據值,指示是時候完成加載更多項目 –