我正在尝试实现一个 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");
我需要什么才能让它工作?
编辑:
我尝试用正确的方法来做,但没有这样的工作......我错过了一些东西......当你的英语不流利时,正确理解博客说明并不容易......
函数.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);
}
})
})
})