[英]Wordpress - how can I fetch more posts via AJAX?
首先,我认为这是一个长期目标,但希望有人可以提供帮助。
为了解释当前的情况,目前我有一个自定义插件,可以获取有关用户及其 4 个最新帖子的各种信息。
我也在使用 WPBook 插件(所以这是在 facebook 上,只是一个典型的 wordpress 网站)
好的,这里是我的代码,它为用户抓取 4 个最近的帖子:
// The Query
query_posts('posts_per_page=4&author='.$blogger_id);
// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;
// the Loop
while (have_posts()) : the_post();
?>
<div class="oe-grid-box">
<a href="<?php the_permalink() ?>" <?php the_title()?></a>
<div class="oe-grid-box-content">
<?php echo the_content( '...' ); ?>
</div>
<div class="oe-grid-pic">
<a href="<?php the_permalink() ?>">
<span class="<?php echo strtolower($category); ?>"></span>
</a>
<?php echo $image; ?>
</div>
</div>
<?php
endwhile;
// Reset Query
wp_reset_query();
?>
<div id="load-more">Load More</div>
我尝试遵循本教程,但不是单独的插件,而是将代码放在我现有的插件中,但现在我的页面无法加载:
http://www.problogdesign.com/wordpress/load-next-wordpress-posts-with-ajax/
理想情况下,我想要的是在单击加载更多按钮时,再获取 4 个帖子并显示它们。
如果有人可以帮助我真的很感激。
更新
好的,
到目前为止,我已经在我的 jQuery 中添加了调用 php 文件,该文件应该有希望返回帖子,但它不起作用。
我想是因为它不知道 WordPress 的功能是什么?
如果我在我的加载更多脚本中放一个简单的echo
,jQuery 成功函数会显示一个警告说它有效,但是如果我开始做 WordPress 的东西,我会收到一个内部服务器错误,这是 load-more.php 文件中的代码:
// load wordpress into template
$path = $_SERVER['DOCUMENT_ROOT'];
define('WP_USE_THEMES', false);
require($path .'/wp-load.php');
$blogger_id = $_GET['id'];
$firstname = $_GET['firstname'];
$surname = $_GET['surname'];
$posts = $_GET['posts'] + 4;
$category = $_GET['category'];
$image = $_GET['image'];
// The Query
query_posts('paged='.get_query_var('paged').'&posts_per_page=' . $posts . '&author='.$blogger_id);
// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;
// then the same loop as in my page that is making the ajax call.
经过一番努力,我找到了答案,这里有一个解决方案,适用于那些卡在同一位置的人。
这会出现在您的插件页面中,您可以在其中获取用户等的帖子。
<script type="text/javascript">
$(document).ready(function() {
posts = 8;
author_posts = parseInt(<?php echo $author_posts; ?>);
$("#link_selector").click(function() {
// alert('posts - ' + posts + 'author posts - ' + author_posts);
if ((posts - author_posts) > 3) {
$("#link_selector").text('No more posts!');
}
else {
var category = '<?php echo strtolower($category); ?>';
var id = '<?php echo $blogger_id; ?>';
var firstname = '<?php echo $firstname; ?>';
var surname = '<?php echo $surname; ?>';
// alert(posts + category + id + firstname + surname);
$.ajax({
type: "GET",
url: "/wordpress/wp-admin/admin-ajax.php",
dataType: 'html',
data: ({ action: 'loadMore', id: id, firstname: firstname, surname: surname, posts: posts, category: category}),
success: function(data){
$('#ajax_results_container').hide().fadeIn('slow').html(data);
posts = posts + 4;
if ((posts - author_posts) > 3) {
$("#link_selector").text('No more posts!');
}
}
});
}
});
});
</script>
然后在您主题的functions.php 中,放置您的函数来执行您的ajax 请求:
function loadMore() {
$blogger_id = $_GET['id'];
// get your $_GET variables sorted out
// setup your query to get what you want
query_posts('posts_per_page=' . $posts . '&author='.$blogger_id);
// initialsise your output
$output = '';
// the Loop
while (have_posts()) : the_post();
// define the structure of your posts markup
endwhile;
// Reset Query
wp_reset_query();
die($output);
}
然后,在函数关闭后,放入动作钩子
add_action('wp_ajax_loadMore', 'loadMore');
add_action('wp_ajax_nopriv_loadMore', 'loadMore'); // not really needed
就是这样!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.