我想实现一个Ajax请求加载更多帖子上的点击。
不过,我已经得到了一个错误:调用未定义功能query_posts()
下面是我的Ajax脚本:
$('#下一容器)。点击(函数(){
变种的ID = [];
$(元素。)每个(函数(){IDs.push(this.id);});
$('#下一容器)HTML('装');
$阿贾克斯({
网址:'的wp-content /主题/ freakyshape / INC / data.php',
键入:POST,
数据:{'post_ids []':标识},
成功:功能(数据){
$ container.isotope('插入',$(数据));
}
});
});
我data.php文件加载后:
< PHP
如果(使用isset($ _ POST ['post_ids'])){
$ IDS =阵列();
$ IDS [] = $ _ POST ['post_ids'];
$ posts_per_page = 8;
全球$ wp_query;
query_posts(阵列('post_type'=>阵列('后','投资'),'post__not_in'=> $ IDS,'posts_per_page'=> $ posts_per_page));
而(have_posts()):the_post();
?>
//我做一些东西呼应;
< PHP
ENDWHILE;
wp_reset_query();
}
?>
和我的functions.php:
$ admin_path = TEMPLATEPATH。 / INC /;
require_once($ admin_pathdata.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(脚本,get_template_directory_uri()'/ JS / load_post.js',阵列('jQuery的'),'1.0',1);
wp_localize_script('脚本','ajax_var',阵列('URL'=> ADMIN_URL(管理-ajax.php),随机数=> wp_create_nonce('AJAX-现时')));
load_post.php:
< PHP
功能load_post_callback(){
如果(使用isset($ _ POST ['post_ids'])){
$随机数= $ _ POST ['随机数'];
如果(!wp_verify_nonce($现时,Ajax的随机数)){
死亡('Interdit!');
}
$ IDS =阵列();
$ IDS [] = $ _ POST ['post_ids'];
$ posts_per_page = 8;
全球$ wp_query;
query_posts(阵列('post_type'=>阵列('后','投资'),'post__not_in'=> $ IDS,'posts_per_page'=> $ posts_per_page));
而(have_posts()):the_post();
$ post_type = get_post_type($后> ID);
如果($ post_type =='后'){
$ post_type ='博客';
}
?>
< DIV CLASS =元素正常<?PHP的echo $ post_type;>中ID =< PHP的回声the_id();?>>
< DIV CLASS =元素容器>
< IMG SRC =< PHP的回声wp_get_attachment_url(get_post_thumbnail_id());?>中类=缩略图/>
< DIV CLASS =元素回>< / DIV>
< DIV CLASS =元素的描述>< PHP的回声the_title(); ?>< / DIV>
< DIV CLASS =要素类><我类=图标>< / I>&安培; NBSP;&安培; NBSP<?PHP的echo $ post_type; ?>< / DIV>
<一类=链接的href =标题=< PHP的回声the_title();?>中的onclick =gestionClic(compteur'id');>
< DIV CLASS =更多>详情< / DIV>
&所述; / a取代;
< / DIV>
< / DIV>
< PHP
ENDWHILE;
wp_reset_query();
}
}
?>
load_post.js:
jQuery的(文件)。就绪(函数(){
VAR URL = ajax_var.url;
VAR随机数= ajax_var.nonce;
jQuery的('#下一容器)。点击(函数(){
变种的ID = [];
$(元素。)每个(函数(){IDs.push(this.id);});
执行console.log(IDS);
$('#下一容器)HTML('装');
$阿贾克斯({
网址:'的wp-content /主题/ freakyshape / INC / load_post.php',
键入:POST,
数据:{'post_ids []':标识},//'post_ids ='+标识,
成功:函数($数据){
$ container.isotope(插入,$数据);
}
})
})
})
解决方案
使用Ajax这种方式是不正确的。为了您的code的工作,你需要包括 WP-load.php
,并使用一些其他的解决方法。该网站是充满了这个可怕的例子。
奥托,核心贡献者,解释了主要原因,我们不包括WP-负载,请。。这些都是:
正确的方法是:
使用 wp_ajax _
挂钩运行PHP code,并返回到JS文件。
使用 wp_localize_script
到PHP变量传递给JS文件。
我的一个工作的例子在字preSS答案。和许多很好的例子,从我们的一个MODS的以及领先贡献者Ajax标签。
I'm trying to implement an Ajax request to load more posts on click.
However I've got an error : Call to undefined function query_posts()
Here is my Ajax script :
$('#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));
}
});
});
my data.php file to load post:
<?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();
}
?>
and in my functions.php:
$admin_path = TEMPLATEPATH . "/inc/";
require_once ($admin_path . "data.php");
What do I need to make it work?
EDIT:
I try with the right way to do it but nothing works like this... I miss something... It's not easy to correctly understand blog instruction when you are not fluent in English...
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);
}
})
})
})
解决方案
Using Ajax this way is not correct. For your code to work, you need to include wp-load.php
and use some other workarounds. The web is full of this awful examples.
Otto, core contributor, explains the main reasons why we Don’t include wp-load, please.. And these are:
The proper way is:
usingwp_ajax_
hooks to run PHP code and return it to the JS file.
using wp_localize_script
to pass PHP variables to the JS file.
One working example of mine at WordPress Answers. And lots of good examples from one of our mods and leading contributor to the Ajax tag.