您需要採取幾個步驟才能做到這一點。首先,您需要使用AJAX來調用您的數據庫並獲取項目(本例中爲posts)。你很可能想要返回一個可以解析的JSON字符串來爲帖子構建你的HTML標記。一個例子是:
$.ajax({
type: "POST",
url: "path/to/load-more.php",
context: document.body,
success: function(data) {
if(!data){
//possible errors
return false;
}
var posts = JSON.parse(data);
var container = document.getElementById('container')
for(var i in posts){
var post = posts[i];
//now post will have info like post.image, post.title
//post.excerpt, and whatever else you put in the php script
//build the post with HTML and append it to the container
//and [reload masonry][2] eaxmple
var div = document.createNode('div');
var h1 = document.createNode('h1');
h1.innerHTML = post.title;
div.appendChild(h1);
container.appendChild(div);
container.masonry('reload');
}
}
});
其次你需要你的load-more.php
作爲JSON字符串返回你的10個職位。您將需要run the WP loop outside of the engine。
<?php
// Include WordPress
define('WP_USE_THEMES', false);
require('/server/path/to/your/wordpress/site/htdocs/blog/wp-blog-header.php');
query_posts('showposts=10');
$arr = new array();
while ($wp_query->have_posts()) : $wp_query->the_post();
//put in whatever info you want from the post in a new
//array index
$arr[the_ID()]['title'] = the_title();
endwhile;
return json_encode(arr);
?>
將ajax調用放入函數中,並在要加載的事件上調用該函數;比如點擊一個按鈕,或者滾動到頁面底部。
我希望這會有所幫助。