Closed. This question needs to be more focused. It is not currently accepting answers.admin管理员组文章数量:1429449
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this questionhow could I add load more posts to my theme? Is it possible to add it without using rest api?
Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this questionhow could I add load more posts to my theme? Is it possible to add it without using rest api?
Share Improve this question edited Apr 29, 2019 at 11:03 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Apr 28, 2019 at 22:25 jowanjowan 33 bronze badges 1- Possible duplicate of Ajax Load More Posts in Category Page – Jacob Peattie Commented Apr 29, 2019 at 7:27
1 Answer
Reset to default 2You can add a load more button using a variety of WordPress plugins, just pick one and follow the instructions. I like this one: https://wordpress/plugins/ajax-load-more-anything/
Yes, it is possible to add it without using the REST API but the REST API is a great use for this type of activity. To provide a concrete example I would need to see the exact page you are working on but the basic approach (NOT using the REST API) is as follows: Add some jQuery code to every page that:
- interrupts the click event
- calls the page via Ajax
- parses out the additional posts
- appends the posts to the end of the current list
So, given the following HTML:
<section id="blogposts">
<article>(blog post HTML)</article>
</section>
<a id="loadmore" href="(href to next page of results)">Load More</a>
You might have jQuery that looks kind of like this (kind of because there are a lot more details you'll need to manage to make this bullet proof - this example just loads the next page of posts):
$("#loadmore").on("click", function(e){
e.preventDefault
var nextPage;
$.ajax({
type: 'GET',
url: nextPage.attr('href'),
success: function(data) {
if (0 == data.length) {
// nothing more to show
} else {
var frag = $.parseHTML(data)).children('#blogposts');
$('#blogposts').append($(frag));
}
},
error: function(data) {
nextPage.text('Error loading more… Click to try again');
}
});
});
The above script isn't perfect but should give you an idea how this can be done without using the REST API.
本文标签: javascriptHow could I add load more posts to my theme
版权声明:本文标题:javascript - How could I add load more posts to my theme? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745542062a2662536.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论