利用AJAX實(shí)現(xiàn)WordPress中的文章列表及評(píng)論的分頁(yè)功能
文章列表頁(yè)分頁(yè)
一.加載 jQuery 庫(kù)
既然是 jQuery 驅(qū)動(dòng)的 Ajax ,加載 jQuery 庫(kù)是必須的。
二.文章列表格式
在你的文章列表頁(yè)面(首頁(yè) 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>
三.加入默認(rèn)導(dǎo)航
因?yàn)?Ajax 分頁(yè)每次獲取的是下一頁(yè)的內(nèi)容,因此只需調(diào)用 WordPress 的默認(rèn)導(dǎo)航。在你的 index.php (或是其他文章列表頁(yè)面)加入以下代碼,生成默認(rèn)的 WordPress 導(dǎo)航。
<div id="pagination"><?php next_posts_link(__('LOAD MORE')); ?></div>
四.Ajax 獲取下一頁(yè)
在你的主題 js 文件里加入以下代碼
// 使用 live() 使 js 對(duì)通過(guò) 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 { // 若沒(méi)有鏈接,即為最后一頁(yè),則移除導(dǎo)航 $("#pagination").remove(); } } }); return false; });
五.滾動(dòng)觸發(fā)翻頁(yè)
如果想當(dāng)鼠標(biāo)滾動(dòng)到接近頁(yè)面底部時(shí)自動(dòng)翻頁(yè),則可以把代碼改成下面的樣式
// 給瀏覽器窗口綁定 scroll 事件 $(window).bind("scroll",function(){ // 判斷窗口的滾動(dòng)條是否接近頁(yè)面底部 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 { // 若沒(méi)有鏈接,即為最后一頁(yè),則移除導(dǎo)航 $("#pagination").remove(); } } }); } });
六.添加導(dǎo)航 css
為導(dǎo)航添加一段 css 美化一下,另外還可以準(zhǔn)備一張 gif 圖來(lái)表示正在加載,下面是 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; }
評(píng)論分頁(yè)
一.準(zhǔn)備
加載 jQuery 庫(kù),這個(gè)不解釋了。
二.開(kāi)啟 WordPress 評(píng)論分頁(yè)
打開(kāi) WordPress 后臺(tái) - 設(shè)置 - 討論,在“其他評(píng)論設(shè)置”中勾選分頁(yè)顯示評(píng)論,設(shè)置一下評(píng)論數(shù)目,這里的評(píng)論數(shù)目?jī)H計(jì)算主評(píng)論,回復(fù)評(píng)論不作計(jì)算。這里我填了比較大的數(shù)字(15),因?yàn)樵u(píng)論分頁(yè)分得太細(xì)會(huì)使用戶不便于閱讀之前的評(píng)論。
在后臺(tái)開(kāi)啟評(píng)論分頁(yè)后,在 comments.php 中需要添加分頁(yè)導(dǎo)航的地方加入以下代碼(如主題中有類似代碼則無(wú)須再添加,另外代碼中的 nav 標(biāo)簽為 HTML5 標(biāo)簽,若主題沒(méi)有使用 HTML5 則有 div 代替即可。)
<nav id="comments-navi"> <?php paginate_comments_links('prev_text=?&next_text=?');?> </nav>
三.評(píng)論分頁(yè)的 SEO
從 SEO 的角度看,評(píng)論分頁(yè)會(huì)造成重復(fù)內(nèi)容(分頁(yè)的內(nèi)容正文都一樣,并且 keywords 和 description 也相同),這樣對(duì)于評(píng)論很多的博客很容易因?yàn)橹貜?fù)內(nèi)容太多而降權(quán),因此需要在 SEO 方面作出一些處理,最為方便有效的方法是使用 meta 標(biāo)簽。在你的 header.php 原有的 meta 標(biāo)簽下加入以下代碼,這樣分頁(yè)的頁(yè)面便會(huì)禁止被搜索引擎收錄,防止內(nèi)容重復(fù)。
<?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 評(píng)論
根據(jù)上文所述,現(xiàn)在主題中已經(jīng)有評(píng)論分頁(yè)了,要做到 Ajax 的評(píng)論分頁(yè),只需 JavaScript 的配合,不過(guò)在這之前首先要在評(píng)論列表前加入一個(gè)元素,用于在顯示新一頁(yè)評(píng)論列表時(shí)表示列表正在加載。假設(shè)主題模板 comments.php 的評(píng)論模塊結(jié)構(gòu)如下:
<div class="comments"> <h3 id="comments-list-title">Comments</h3> <!-- 顯示正在加載新評(píng)論 --> <div id="loading-comments"><span>Loading...</span></div> <!-- 評(píng)論列表 --> <ol class="comment_list"> <li>...</li> <li>...</li> <li>...</li> </ol> <!-- 評(píng)論分頁(yè)導(dǎo)航 --> <nav id="comments-navi"> <a class="prev page-numbers" href="#">1</a> ... </nav> </div>
在你的 js 文件中加入以下 js 代碼實(shí)現(xiàn)評(píng)論分頁(yè)
// 評(píng)論分頁(yè) $body=(window.opera)?(document.compatMode=="CSS1Compat"?$('html'):$('body')):$('html,body'); // 點(diǎn)擊分頁(yè)導(dǎo)航鏈接時(shí)觸發(fā)分頁(yè) $('#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)文章
微信小程序?qū)崿F(xiàn)數(shù)字滾動(dòng)動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)數(shù)字滾動(dòng)動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07JavaScript實(shí)現(xiàn)簡(jiǎn)單圖片翻轉(zhuǎn)的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單圖片翻轉(zhuǎn)的方法,涉及javascript操作圖片與數(shù)組的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04js實(shí)現(xiàn)簡(jiǎn)單的點(diǎn)名器隨機(jī)色實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于js實(shí)現(xiàn)簡(jiǎn)單的點(diǎn)名器隨機(jī)色的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09JavaScript實(shí)現(xiàn)微信小程序打卡時(shí)鐘項(xiàng)目實(shí)例
這篇文章主要為大家介紹了JavaScript實(shí)現(xiàn)微信小程序打卡時(shí)鐘項(xiàng)目實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04