2013-12-18 67 views
0

我使用以下操作將帖子加載到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

+0

將所有鏈接存儲在數組中 – mplungjan

+0

傳遞服務器返回的數據值,指示是時候完成加載更多項目 –

回答

0
var lastLink; 

$('#almc-load-more').click(function(e) { 
    if ($anchor == $('.navigation a.next').last()) { 
     lastLink = 1; 
    } 

    if (lastLink == 1) {return} else { 
     ... 
    } 
... 
0

你可以添加一些代碼來檢查新href是比目前的HREF不同,然後只嘗試,如果添加一個新的崗位,他們不一樣。然後,如果他們不是,你可以有一條消息說沒有更多的帖子。

+0

是的。好點子。需要有話要說'沒有更多的帖子'。我會看看我想出了什麼。謝謝 – Victor

相關問題