嘿,伙计们,最近几天我一直在想怎么做。如前所述http://net.tutsplus.com/tutorials/javascript-ajax/create-a-twitter-like-load-more-widget/ 但在wordpress内部。我终于想出了一个解决方案,可能不是最好的,但我希望你们可以改进我的代码,并像我一样发布在这里。您可以在任何地方随意使用它。(我的编码技术真的很差,所以
首先,需要在标题中包含jquery。
在要显示帖子的页面上插入以下代码:
<?php
session_start();
$number_of_posts = 3;
if (!$_SESSION[\'posts_start\']) { $_SESSION[\'posts_start\'] = 0; }
$_SESSION[\'posts_start\'] = $_SESSION[\'posts_start\'] ? $_SESSION[\'posts_start\'] : $number_of_posts;
$total_posts = wp_count_posts(\'post\');
$total_posts = $total_posts->publish;
?>
<script type="text/javascript">
jQuery(document).ready(function(){
var initialPosts = <?php echo my_get_posts(0, 5); ?>;
var postHandler = function(postsJSON) {
jQuery.each(postsJSON,function(i,post) {
// Salva as variáveis de cada post
var postURL = post.permalink;
var postCOMMENTS = post.comments;
var postTITLE = post.title;
var postDATE = post.date;
var postAUTHOR = post.author;
// Coloca as variáveis na lista
jQuery(".lista-artigos")
.append("<li><span class=\\"comentarios-artigo\\">" + postCOMMENTS + "</span><h2><a href=\\"" + postURL +"\\">" + postTITLE + "</a></h2><span class=\\"data-artigo\\">Postado em " + postDATE + " por " + postAUTHOR + "</span></li>")
});
};
postHandler(initialPosts);
var totalposts = <?php echo $total_posts; ?>;
var start = <?php echo $_SESSION[\'posts_start\']; ?>;
var desiredPosts = <?php echo $number_of_posts; ?>;
jQuery(\'#load-more\').click(function(){
jQuery(\'#load-more\').removeClass(\'carregar\').addClass(\'ativado\').text(\'Loading...\');
jQuery.ajax({
url: \'http://yoursite/wp-content/themes/yourtheme/mais-posts.php\',
data: {
\'start\': start,
\'desiredPosts\': desiredPosts
},
type: \'get\',
dataType: \'json\',
cache: false,
success: function(responseJSON) {
jQuery(\'#load-more\').text(\'Load More\');
start += desiredPosts;
postHandler(responseJSON);
},
error: function() {
jQuery(\'#load-more\').text(\'Ops! Try Again.\');
},
complete: function() {
jQuery(\'#load-more\').removeClass(\'ativado\').addClass(\'carregar\');
if (totalposts <= start) { jQuery(\'#load-more\').hide(); }
}
});
});
});
</script>
使用以下代码在您的主题文件夹(我的名为mais posts.php)中创建一个php文件:
<?php
require( \'../../../wp-load.php\' );
if(isset($_GET[\'start\'])) {
echo my_get_posts($_GET[\'start\'], $_GET[\'desiredPosts\']);
$_SESSION[\'posts_start\'] += $_GET[\'desiredPosts\'];
exit();
}
?>
我希望它可以帮助任何正在寻找这种东西的人。
因此,如果您对此代码有任何建议,请在下面发布。