我正在尝试通过ajax点击获取更多帖子。在我的函数中,我对脚本进行了本地化
wp_enqueue_script( \'news\', get_template_directory_uri().\'/news/js/news.js\', \'\', \'\', true );
//Localise script for ajax call
wp_localize_script( \'news\', \'ajax_posts\', array(
\'ajaxurl\' => admin_url( \'admin-ajax.php\' ),
\'noposts\' => \'No Older Posts Found\',
));
我的帖子查询功能
function more_post_ajax(){
$ppp = (isset($_POST["ppp"])) ? $_POST["ppp"] : 2;
$page = (isset($_POST[\'pageNumber\'])) ? $_POST[\'pageNumber\'] : 0;
header("Content-Type: text/html");
$args = array(
\'suppress_filters\' => true,
\'post_type\' => \'post\',
\'posts_per_page\' => $ppp,
\'paged\' => $page
);
$loop = new WP_Query($args);
$out = \'\';
if ($loop -> have_posts()) : while ($loop -> have_posts()) : $loop -> the_post();
$out .=\'<article class="post-container clearfix">
<div class="post-inner clearfix">
<div class="post-image">
\'.get_the_post_thumbnail().\'
</div>
<h1>\'.the_title().\'</h1>
<span class="date">Date: \'.get_the_date().\'</span>
<br>
<span class="author">Author: \'.the_author().\'</span>
<br>
\'.get_custom_excerpt(get_the_content()).\'
<a class="read-more" href="\'.get_the_permalink().\'" title="Read More"><img src="\'.get_template_directory_uri().\'/news/img/read-more.png" alt="Read More"></a>
</div>
</article>\';
endwhile;
endif;
wp_reset_postdata();
echo $out;
}
add_action(\'wp_ajax_nopriv_more_post_ajax\', \'more_post_ajax\');
add_action(\'wp_ajax_more_post_ajax\', \'more_post_ajax\');
然后是我的jQuery
jQuery(document).ready(function(){
var ppp = 2; // Post per page
var pageNumber = 1;
function load_posts(){
pageNumber++;
var str = \'&pageNumber=\' + pageNumber + \'&ppp=\' + ppp + \'&action=more_post_ajax\';
jQuery.ajax({
type: "POST",
dataType: "html",
url: ajax_posts.ajaxurl,
data: str,
success: function(data){
console.log(data);
var $data = jQuery(data);
if($data.length){
jQuery(".posts-wrapper").append($data);
jQuery(".load-more").attr("disabled",false);
} else{
jQuery(".load-more").attr("disabled",true);
}
},
error : function(jqXHR, textStatus, errorThrown) {
$loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
}
});
return false;
}
jQuery(".load-more").on("click",function(e){ // When btn is pressed.
jQuery(".load-more").attr("disabled",true); // Disable the button, temp.
load_posts();
e.preventDefault();
});
});
ajax响应始终为0。谁能帮忙吗。
SO网友:Anton Pedan
在PHP函数的末尾尝试die()函数。这会有帮助的。
UPD:Ive made some simple version of your problem code to check it and it works. I think it will helps you.
You are able to change inner PHP script to your logic and it won
t返回0。
PHP:
function more_post_ajax(){
echo "Hello";
die();
}
add_action(\'wp_ajax_nopriv_more_post_ajax\', \'more_post_ajax\');
add_action(\'wp_ajax_more_post_ajax\', \'more_post_ajax\');
function custom_scripts_init(){
wp_enqueue_script( \'custom\', get_template_directory_uri().\'/assets/js/custom.js\', array(\'jquery\') );
wp_localize_script( \'custom\', \'ajaxPosts\', array(
\'customUrl\' => admin_url( \'admin-ajax.php\' ),
\'noposts\' => \'No Older Posts Found\',
));
}
add_action(\'wp_footer\', \'custom_scripts_init\');
JQuery:
jQuery(document).ready(function(){
jQuery("#page").append("<button type=\'button\' class=\'load-more\'/>");
var ppp = 2; // Post per page
var pageNumber = 1;
function load_posts(){
pageNumber++;
var str = \'&pageNumber=\' + pageNumber + \'&ppp=\' + ppp + \'&action=more_post_ajax\';
jQuery.ajax({
type: "POST",
dataType: "html",
url: ajaxPosts.customUrl,
data: str,
success: function(data){
console.log(data);
},
error : function(jqXHR, textStatus, errorThrown) {
//$loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
}
});
return false;
}
jQuery(".load-more").on("click",function(e){ // When btn is pressed.
// jQuery(".load-more").attr("disabled",true); // Disable the button, temp.
load_posts();
e.preventDefault();
});
});