WordPress点击加载更多文章

首先找到index.php有些可能是loop.php看主题的自定义然后把主题默认的分页导航的容器换成下面的容器。

<?php next_posts_link(__('点击查看更多')); ?>

然后找到你主题的其中一个js文件,或者自己新建一个js文件都可以,把下面的代码丢进去就可以了,理论上来说支持各种程序,emlog或者tp都支持。

//点击加载更多
jQuery(document).ready(function($) {
    //点击下一页的链接(即那个a标签)
    $('#pagination a').click(function() {
        $this = $(this);
        $this.addClass('loading').text("正在努力加载"); //给a标签加载一个loading的class属性,可以用来添加一些加载效果
        var href = $this.attr("href"); //获取下一页的链接地址
        if (href != undefined) { //如果地址存在
            $.ajax({ //发起ajax请求
                url: href, //请求的地址就是下一页的链接
                type: "get", //请求类型是get
                error: function(request) {
                    //如果发生错误怎么处理
                },
                success: function(data) { //请求成功
                    $this.removeClass('loading').text("点击查看更多"); //移除loading属性
                    var $res = $(data).find(".blockGroup .post-list"); //从数据中挑出文章数据,请根据实际情况更改
                    $('.blockGroup').append($res.fadeIn(500)); //将数据加载加进posts-loop的标签中。
                    var newhref = $(data).find("#pagination a").attr("href"); //找出新的下一页链接
                    if (newhref != undefined) {
                        $("#pagination a").attr("href", newhref);
                    } else {
                        $("#pagination a").remove(); //如果没有下一页了,隐藏
                    }
                }
            });
        }
        return false;
    });
});

css美化:把代码扔进你主题的style.css文件里面就可以了,教程到此结束。

#pagination {
    display: inline-block;
    position: relative;
    height: 30px;
    margin-bottom: 20px;
    padding: 2px 16px;
    color: rgba(0,0,0,.44);
    background: rgba(0,0,0,0);
    font-size: 15px;
    text-align: center;
    text-decoration: none;
    cursor: pointer;
    border: 1px solid rgba(0,0,0,.05);
    vertical-align: bottom;
    white-space: nowrap;
    text-rendering: auto;
    box-sizing: border-box;
    border-radius: 999em;
}
Comments: 21

「人生在世,留句话给我吧」

提交评论