利用AJAX實現(xiàn)WordPress中的文章列表及評論的分頁功能
文章列表頁分頁
一.加載 jQuery 庫
既然是 jQuery 驅(qū)動的 Ajax ,加載 jQuery 庫是必須的。
二.文章列表格式
在你的文章列表頁面(首頁 index.php、歸檔 archive.php )需要確保有以下類似的結(jié)構(gòu)
<!-- 包含所有文章的容器 --> <div id="content"> <!-- 各文章的容器 --> <div class="post"></div> <div class="post"></div> <div class="post"></div> <div class="post"></div> <div class="post"></div> </div>
三.加入默認導航
因為 Ajax 分頁每次獲取的是下一頁的內(nèi)容,因此只需調(diào)用 WordPress 的默認導航。在你的 index.php (或是其他文章列表頁面)加入以下代碼,生成默認的 WordPress 導航。
<div id="pagination"><?php next_posts_link(__('LOAD MORE')); ?></div>
四.Ajax 獲取下一頁
在你的主題 js 文件里加入以下代碼
// 使用 live() 使 js 對通過 Ajax 獲得的新內(nèi)容仍有效 $("#pagination a").live("click", function(){ $(this).addClass("loading").text("LOADING..."); $.ajax({ type: "POST", url: $(this).attr("href") + "#content", success: function(data){ result = $(data).find("#content .post"); nextHref = $(data).find("#pagination a").attr("href"); // 漸顯新內(nèi)容 $("#content").append(result.fadeIn(300)); $("#pagination a").removeClass("loading").text("LOAD MORE"); if ( nextHref != undefined ) { $("#pagination a").attr("href", nextHref); } else { // 若沒有鏈接,即為最后一頁,則移除導航 $("#pagination").remove(); } } }); return false; });
五.滾動觸發(fā)翻頁
如果想當鼠標滾動到接近頁面底部時自動翻頁,則可以把代碼改成下面的樣式
// 給瀏覽器窗口綁定 scroll 事件 $(window).bind("scroll",function(){ // 判斷窗口的滾動條是否接近頁面底部 if( $(document).scrollTop() + $(window).height() > $(document).height() - 10 ) { $(this).addClass('loading').text('LOADING...'); $.ajax({ type: "POST", url: $(this).attr("href") + "#content", success: function(data){ result = $(data).find("#content .post"); nextHref = $(data).find("#pagination a").attr("href"); // 漸顯新內(nèi)容 $("#content").append(result.fadeIn(300)); $("#pagination a").removeClass("loading").text("LOAD MORE"); if ( nextHref != undefined ) { $("#pagination a").attr("href", nextHref); } else { // 若沒有鏈接,即為最后一頁,則移除導航 $("#pagination").remove(); } } }); } });
六.添加導航 css
為導航添加一段 css 美化一下,另外還可以準備一張 gif 圖來表示正在加載,下面是 Melody 的樣式:
#pagination {padding: 20px 0 0 30px; } #pagination .nextpostslink {width: 600px; color: #333; text-decoration: none; display: block; padding: 9px 0; text-align: center; font-size: 14px; } #pagination .nextpostslink:hover {background-color: #cccccc; text-decoration: none; border-radius: 5px; -moz-border-radius: 5px; -webkit-border-radius: 5px; } #pagination .loading {background: url("images/loading.gif") 240px 9px no-repeat; color: #555; } #pagination .loading:hover {background-color: transparent; cursor: default; }
評論分頁
一.準備
加載 jQuery 庫,這個不解釋了。
二.開啟 WordPress 評論分頁
打開 WordPress 后臺 - 設(shè)置 - 討論,在“其他評論設(shè)置”中勾選分頁顯示評論,設(shè)置一下評論數(shù)目,這里的評論數(shù)目僅計算主評論,回復評論不作計算。這里我填了比較大的數(shù)字(15),因為評論分頁分得太細會使用戶不便于閱讀之前的評論。
在后臺開啟評論分頁后,在 comments.php 中需要添加分頁導航的地方加入以下代碼(如主題中有類似代碼則無須再添加,另外代碼中的 nav 標簽為 HTML5 標簽,若主題沒有使用 HTML5 則有 div 代替即可。)
<nav id="comments-navi"> <?php paginate_comments_links('prev_text=?&next_text=?');?> </nav>
三.評論分頁的 SEO
從 SEO 的角度看,評論分頁會造成重復內(nèi)容(分頁的內(nèi)容正文都一樣,并且 keywords 和 description 也相同),這樣對于評論很多的博客很容易因為重復內(nèi)容太多而降權(quán),因此需要在 SEO 方面作出一些處理,最為方便有效的方法是使用 meta 標簽。在你的 header.php 原有的 meta 標簽下加入以下代碼,這樣分頁的頁面便會禁止被搜索引擎收錄,防止內(nèi)容重復。
<?php if( is_single() || is_page() ) { if( function_exists('get_query_var') ) { $cpage = intval(get_query_var('cpage')); $commentPage = intval(get_query_var('comment-page')); } if( !empty($cpage) || !empty($commentPage) ) { echo '<meta name="robots" content="noindex, nofollow" />'; echo "\n"; } } ?>
四.Ajax 評論
根據(jù)上文所述,現(xiàn)在主題中已經(jīng)有評論分頁了,要做到 Ajax 的評論分頁,只需 JavaScript 的配合,不過在這之前首先要在評論列表前加入一個元素,用于在顯示新一頁評論列表時表示列表正在加載。假設(shè)主題模板 comments.php 的評論模塊結(jié)構(gòu)如下:
<div class="comments"> <h3 id="comments-list-title">Comments</h3> <!-- 顯示正在加載新評論 --> <div id="loading-comments"><span>Loading...</span></div> <!-- 評論列表 --> <ol class="comment_list"> <li>...</li> <li>...</li> <li>...</li> </ol> <!-- 評論分頁導航 --> <nav id="comments-navi"> <a class="prev page-numbers" href="#">1</a> ... </nav> </div>
在你的 js 文件中加入以下 js 代碼實現(xiàn)評論分頁
// 評論分頁 $body=(window.opera)?(document.compatMode=="CSS1Compat"?$('html'):$('body')):$('html,body'); // 點擊分頁導航鏈接時觸發(fā)分頁 $('#comments-navi a').live('click', function(e){ e.preventDefault(); $.ajax({ type: "GET", url: $(this).attr('href'), beforeSend: function(){ $('#comments-navi').remove(); $('.comment_list').remove(); $('#loading-comments').slideDown(); $body.animate({scrollTop: $('#comments-list-title').offset().top - 65}, 800 ); }, dataType: "html", success: function(out){ result = $(out).find('.comment_list'); nextlink = $(out).find('#comments-navi'); $('#loading-comments').slideUp('fast'); $('#loading-comments').after(result.fadeIn(500)); $('.comment_list').after(nextlink); } }); });
加載條的 css (僅供參考)
#loading-comments {display: none; width: 100%; height: 45px; background: #a0d536; text-align: center; color: #fff; font-size: 22px; line-height: 45px; }
相關(guān)文章
JavaScript實現(xiàn)簡單圖片翻轉(zhuǎn)的方法
這篇文章主要介紹了JavaScript實現(xiàn)簡單圖片翻轉(zhuǎn)的方法,涉及javascript操作圖片與數(shù)組的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04JavaScript實現(xiàn)微信小程序打卡時鐘項目實例
這篇文章主要為大家介紹了JavaScript實現(xiàn)微信小程序打卡時鐘項目實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04