简体   繁体   English

jQuery加载具有自定义帖子类型的Wordpress的更多按钮

[英]Jquery load more button for Wordpress with Custom post types

I am trying to add a load more button to my wordpress website, to load my custom post types. 我正在尝试向我的wordpress网站添加更多加载按钮,以加载我的自定义帖子类型。 So far I have been able to make it load the posts. 到目前为止,我已经能够使其加载帖子。

The problem is that every time I load more posts it replaces the first batch witch the second and so on. 问题是,每次我加载更多帖子时,它将替换第一批,第二批,依此类推。 I want it to append every consecutive batch of posts to the first batch until the are no more (max_num_pages) posts left after that the button should deactivate. 我希望它将每个连续的帖子批追加到第一批,直到该按钮应停用后再没有(max_num_pages)个帖子为止。

How would I go about achieving this? 我将如何实现这一目标? Suggestions and improvements will be greatly appreciated. 建议和改进将不胜感激。 This is what I have so far. 到目前为止,这就是我所拥有的。

<div id="div1"></div>
<button class="load-more">load more</button>

var clicks = 0;

$(".load-more").click(function(){
clicks += 1;

if(clicks < 2){
$("#div1").load("http://mysite.nl/loadmore/");
    } else if(clicks > 1) {
        $("#div1").load("http://mysite.nl/loadmore/page/" + clicks);
    }
});

Using $("#div1").load() will replace all the HTML in #div1 . 使用$("#div1").load()将替换#div1所有HTML。 I'd say load your new data on a hidden element then .append() that new data to #div1 . 我会说将您的新数据加载到一个隐藏元素上,然后将该新数据.append()加载到#div1

Or 要么

...perhaps you can save the existing #div1 HTML to a variable: ...也许您可以将现有的#div1 HTML保存到变量中:

var foo = $('#div1').html();

then load your new data and afterwards .prepend() the original: 然后加载您的新数据,然后.prepend()原始数据:

$('#div1')
    .load("http://...")
    .prepend(foo);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM