【发布时间】:2013-07-05 22:18:51
【问题描述】:
我正在尝试实现一个 Ajax 请求以在点击时加载更多帖子。
但是我有一个错误:Call to undefined function query_posts()
这是我的 Ajax 脚本:
$('#next-container').click(function() {
var IDs = [];
$(".element").each(function(){ IDs.push(this.id); });
$('#next-container').html('loading');
$.ajax({
url: 'wp-content/themes/freakyshape/inc/data.php',
type: 'POST',
data: {'post_ids[]': IDs },
success: function(data) {
$container.isotope( 'insert', $(data));
}
});
});
我要加载帖子的 data.php 文件:
<?php
if (isset($_POST['post_ids'])) {
$ids = array();
$ids[] = $_POST['post_ids'];
$posts_per_page = 8;
global $wp_query;
query_posts(array('post_type' => array('post', 'portfolio'), 'post__not_in' => $ids, 'posts_per_page' => $posts_per_page));
while (have_posts()) : the_post();
?>
// I do some stuff echo;
<?php
endwhile;
wp_reset_query();
}
?>
在我的functions.php中:
$admin_path = TEMPLATEPATH . "/inc/";
require_once ($admin_path . "data.php");
我需要什么才能让它工作?
编辑:
我尝试了正确的方法,但没有这样的工作......我错过了一些东西......当你的英语不流利时,要正确理解博客说明并不容易......
function.php:
add_action('wp_ajax_load_post', 'load_post_callback');
add_action('wp_ajax_nopriv_load_post', 'load_post_callback');
wp_enqueue_script('script', get_template_directory_uri().'/js/load_post.js', array('jquery'), '1.0', 1 );
wp_localize_script('script', 'ajax_var', array('url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('ajax-nonce')));
load_post.php:
<?php
function load_post_callback() {
if (isset($_POST['post_ids'])) {
$nonce = $_POST['nonce'];
if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) ) {
die ( 'Interdit !');
}
$ids = array();
$ids[] = $_POST['post_ids'];
$posts_per_page = 8;
global $wp_query;
query_posts(array('post_type' => array('post', 'portfolio'), 'post__not_in' => $ids, 'posts_per_page' => $posts_per_page));
while (have_posts()) : the_post();
$post_type = get_post_type( $post->ID );
if ($post_type == 'post') {
$post_type = 'blog';
}
?>
<div class="element normal <?php echo $post_type; ?>" id="<?php echo the_id(); ?>">
<div class="element-container">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id()); ?>" class="thumbnail" />
<div class="element-back"></div>
<div class="element-description"><?php echo the_title(); ?></div>
<div class="element-category"><i class="icon"></i>   <?php echo $post_type; ?></div>
<a class="link" href="" title="<?php echo the_title(); ?>" onclick="gestionClic(compteur'id');">
<div class="more">more.</div>
</a>
</div>
</div>
<?php
endwhile;
wp_reset_query();
}
}
?>
load_post.js:
jQuery(document).ready(function() {
var url = ajax_var.url;
var nonce = ajax_var.nonce;
jQuery('#next-container').click(function() {
var IDs = [];
$(".element").each(function(){ IDs.push(this.id); });
console.log(IDs);
$('#next-container').html('loading');
$.ajax({
url: 'wp-content/themes/freakyshape/inc/load_post.php',
type: 'POST',
data: {'post_ids[]': IDs },//'post_ids='+IDs,
success: function($data) {
$container.isotope( 'insert', $data);
}
})
})
})
【问题讨论】:
-
我建议您查看WP-endorsed way to use AJAX - 我敢打赌,如果不走这条路线,您会丢失一些基本的 WP 核心。
-
您的 data.php 是一个自己的 php 文件。它没有包含任何 wordpress 的数据。
-
谢谢,但我怎样才能将我的自定义 php 文件包含到 wordpress 核心中,以使其能够使用 wordpress 功能?我错过了 wordpress 文档中的一些内容...
-
使用 WP 自己的管理 ajax php 文件并使用它来引用主题的 functions.php 文件中的函数,而不是包含外部 php 文件。否则,您必须在 data.php 中加载 WP 核心。
-
One example 了解如何使 Ajax 成为 WordPress 的方式。另外,don't use
query_posts.