客户端:
<input id="cat" name="cat" type="hidden" value="<? echo $category->slug; ?>"/>
<input id="limit" name="limit" type="hidden" value="0"/>
<input id="count" name="count" type="hidden" value="5"/>
<input id="over" name="over" type="hidden" value="0"/>
<input id="length" name="length" type="hidden" value="30"/>
<input id="basturl" name="basturl" type="hidden" value="<? bloginfo('url'); ?>/?<? echo $category->slug; ?>="/>
<div class="btn-more">点击加载5条资讯</div>
<script>
function get_post()
{
$.getJSON(
"http://www.xxx.com/ajax-wap-post?callback=?",
{ cat: $("#cat").val(), limit:$("#limit").val(), count:$("#count").val(), length:$("#length").val() },
function(data)
{
if(data.result == 0)
{
$(".btn-more").html("没有更多了……");
$("#over").val("1");
}else
{
var html = "";
for(var i = 0; i<data.posts.length;i++)
{
html = html + "<li><a href='" + $("#basturl").val() + data.posts[i].id+"'>"+data.posts[i].title+"</a><span>"+data.posts[i].date+"</span></li>";
}
$(".catlist ul").append(html);
$(".btn-more").html("点击加载5条资讯");
$("#limit").val(parseInt($("#limit").val()) + parseInt(data.result));
}
},
"json"
);
}
get_post();
$(".btn-more").click(function(){
if($("#over").val() == "1"){return;} $(".btn-more").html("加载中……"); get_post();
});
</script>
服务器端:
<?php global $wpdb; if(!$_GET)
{ exit; } $cat = htmlspecialchars(trim($_GET["cat"]));
$limit = htmlspecialchars(trim($_GET["limit"]));
$count = htmlspecialchars(trim($_GET["count"]));
$length = htmlspecialchars(trim($_GET["length"])); $args = array('numberposts' => $count, 'offset'=> $limit, 'category' => get_category_by_slug($cat)->term_id,'post_status' => 'publish');
$posts = get_posts($args);
$ary = array('result'=>count($posts),'posts'=>array()); foreach( $posts as $post ) {
array_push($ary["posts"],array('id'=>$post->ID,'title'=> mb_strimwidth($post->post_title, 0, $length, '…','utf-8') ,'date'=> date('Y-m-d',strtotime($post->post_date))));
} echo $_GET['callback'].'('.json_encode($ary).')';
exit;
?>