文章列表页分页
一.加载 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; }

点赞(90)

评论列表共有 0 条评论

立即
投稿
返回
顶部