WordPress – 如何通过AJAX获取更多post?
首先,我认为这是一个远景,但希望有人可以提供帮助。
为了解释目前的情况,目前我有一个自定义插件,可以获取有关用户及其最近4个post的各种信息。
我也在使用WPBook插件(所以这是在Facebook上,只是一个典型的wordpress网站)
好的,所以这里是我的代码,它抓住了用户的4个最新post:
// 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(); ?> <a href="https://stackoverflow.com/questions/11887760/wordpress-how-can-i-fetch-more-posts-via-ajax/" <a href="https://stackoverflow.com/questions/11887760/wordpress-how-can-i-fetch-more-posts-via-ajax/"> <span class=""> Load More
我尝试按照本教程,但不是单独的插件,将代码放在我现有的插件中,但现在我的页面将无法加载:
理想情况下,我想要的是当点击加载更多按钮时,再获取4个post并显示它们。
如果有人可以提供帮助,我会非常感激。
UPDATE
好,
到目前为止,我已经在我的jQuery中添加了调用php文件,该文件应该有希望返回post,但它不起作用。
我在想这是因为它不知道WordPress的function是什么?
如果我在我的加载更多脚本中放置一个简单的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.
经过大量的努力,我找到了答案,这里是那些陷入同一位置的人的解决方案。
这将在您的插件页面中,您将获得用户的post等。
然后在你的主题的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
而已!