国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

利用AJAX實現(xiàn)WordPress中的文章列表及評論的分頁功能

2019-11-20 10:01:17
字體:
供稿:網(wǎng)友

文章列表頁分頁
一.加載 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>

三.加入默認導(dǎo)航
因為 Ajax 分頁每次獲取的是下一頁的內(nèi)容,因此只需調(diào)用 WordPress 的默認導(dǎo)航。在你的 index.php (或是其他文章列表頁面)加入以下代碼,生成默認的 WordPress 導(dǎo)航。

<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 {        // 若沒有鏈接,即為最后一頁,則移除導(dǎo)航          $("#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 {      // 若沒有鏈接,即為最后一頁,則移除導(dǎo)航        $("#pagination").remove();      }    }  });}});

 

六.添加導(dǎo)航 css
為導(dǎo)航添加一段 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ù)目僅計算主評論,回復(fù)評論不作計算。這里我填了比較大的數(shù)字(15),因為評論分頁分得太細會使用戶不便于閱讀之前的評論。

在后臺開啟評論分頁后,在 comments.php 中需要添加分頁導(dǎo)航的地方加入以下代碼(如主題中有類似代碼則無須再添加,另外代碼中的 nav 標簽為 HTML5 標簽,若主題沒有使用 HTML5 則有 div 代替即可。)

<nav id="comments-navi">  <?php paginate_comments_links('prev_text=?&next_text=?');?></nav>

三.評論分頁的 SEO
從 SEO 的角度看,評論分頁會造成重復(fù)內(nèi)容(分頁的內(nèi)容正文都一樣,并且 keywords 和 description 也相同),這樣對于評論很多的博客很容易因為重復(fù)內(nèi)容太多而降權(quán),因此需要在 SEO 方面作出一些處理,最為方便有效的方法是使用 meta 標簽。在你的 header.php 原有的 meta 標簽下加入以下代碼,這樣分頁的頁面便會禁止被搜索引擎收錄,防止內(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 評論
根據(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>  <!-- 評論分頁導(dǎo)航 -->  <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');// 點擊分頁導(dǎo)航鏈接時觸發(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 (僅供參考)

復(fù)制代碼 代碼如下:

#loading-comments {display: none; width: 100%; height: 45px; background: #a0d536; text-align: center; color: #fff; font-size: 22px; line-height: 45px; }

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 正蓝旗| 霍城县| 东丽区| 建始县| 赤峰市| 永新县| 甘洛县| 金寨县| 商南县| 栾川县| 丹江口市| 芦山县| 武功县| 邮箱| 油尖旺区| 永川市| 即墨市| 邵东县| 武城县| 兰州市| 陵水| 东丽区| 岳阳县| 东安县| 府谷县| 正镶白旗| 通海县| 义乌市| 吉安市| 富锦市| 玉溪市| 沾化县| 行唐县| 朝阳县| 饶阳县| 马龙县| 兴城市| 茌平县| 定南县| 中方县| 嘉兴市|