利用AJAX实现WordPress中的文章列表及评论的分页功能


本文整理自网络,侵删。

文章列表页分页
一.加载 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 库,这个不解释了。

阅读剩余部分

相关阅读 >>

php版本升级到7.x后wordpress的一些修改及wordpress技巧

通过自定义字段重新排序 wordpress 文章方法

禁用wordpress的自动保存草稿文章修订功能方法详解

wordpress 不同分类目录调用不同模板的方法

分享十五个最佳jquery 幻灯插件和教程

wordpress实现发布文章自动ping 百度

详解黑客修改wordpress核心文件,劫持网站流量

wordpress中&quot;无法将上传的文件移动至&quot;错误的解决方法

wordpress实现的首页幻灯片展示功能示例【附demo源码】

wordpress博客seo更加完美的6个技巧

更多相关阅读请进入《wordpress》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...