利用AJAX實現WordPress中的文章列表及評論的分頁功能_javascript技巧

來源:互聯網
上載者:User

文章列表頁分頁
一.載入 jQuery 庫
既然是 jQuery 驅動的 Ajax ,載入 jQuery 庫是必須的。

二.文章列表格式
在你的文章列表頁面(首頁 index.php、歸檔 archive.php )需要確保有以下類似的結構

<!-- 包含所有文章的容器 --><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 分頁每次擷取的是下一頁的內容,因此只需調用 WordPress 的預設導航。在你的 index.php (或是其他文章列表頁面)加入以下代碼,產生預設的 WordPress 導航。

<div id="pagination"><?php next_posts_link(__('LOAD MORE')); ?></div>

四.Ajax 擷取下一頁
在你的主題 js 檔案裡加入以下代碼

// 使用 live() 使 js 對通過 Ajax 獲得的新內容仍有效  $("#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");        // 漸顯新內容        $("#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;  });

 

五.滾動觸發翻頁
如果想當滑鼠滾動到接近頁面底部時自動翻頁,則可以把代碼改成下面的樣式

// 給瀏覽器視窗綁定 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");      // 漸顯新內容      $("#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 後台 - 設定 - 討論,在“其他評論設定”中勾選分頁顯示評論,設定一下評論數目,這裡的評論數目僅計算主評論,回複評論不作計算。這裡我填了比較大的數字(15),因為評論分頁分得太細會使使用者不便於閱讀之前的評論。

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

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

三.評論分頁的 SEO
從 SEO 的角度看,評論分頁會造成重複內容(分頁的內容本文都一樣,並且 keywords 和 description 也相同),這樣對於評論很多的部落格很容易因為重複內容太多而降權,因此需要在 SEO 方面作出一些處理,最為方便有效方法是使用 meta 標籤。在你的 header.php 原有的 meta 標籤下加入以下代碼,這樣分頁的頁面便會禁止被搜尋引擎收錄,防止內容重複。

<?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 評論
根據上文所述,現在主題中已經有評論分頁了,要做到 Ajax 的評論分頁,只需 JavaScript 的配合,不過在這之前首先要在評論列表前加入一個元素,用於在顯示新一頁評論列表時表示列表正在載入。假設主題模板 comments.php 的評論模組結構如下:

<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 代碼實現評論分頁

// 評論分頁$body=(window.opera)?(document.compatMode=="CSS1Compat"?$('html'):$('body')):$('html,body');// 點擊分頁導航連結時觸發分頁$('#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; }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.