我正在尝试使用ajax加载更多内容。当我注销时,脚本不会返回任何内容。
这是我的php代码
<?php
function get_blog_items()
{
$offset = (int)intval($_POST[\'offset\']);
$args = array(
\'posts_per_page\' => 4,
\'post_type\' => \'post\',
\'offset\' => $offset
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
get_template_part( \'template-parts/list\', \'blog\' );
endwhile;
endif;
wp_reset_postdata();
}
function add_ajax_actions() {
add_action( \'wp_ajax_nopriv_get_blog_items\', \'get_blog_items\' );
}
add_action( \'admin_init\', \'add_ajax_actions\' );
和我的javascript
$(document).ready(function($) {
var loadbuttonBlogItems = $(\'#loadmore-blog-items\');
loadbuttonBlogItems.on(\'click\', function() {
$(this).addClass(\'loading\');
$.ajax({
url: ajax_object.ajaxurl,
type: \'POST\',
dataType: \'html\',
data: {
action: \'get_blog_items\',
offset: $(\'body > div.page-wrap > section.blog-listing > ul\').children().length
},
})
.done(function(data) {
console.log(data);
$(\'.blog-listing ul\').append($.parseHTML(data))
})
.fail(function() {
console.log("error occured while loading more blog items");
})
.always(function() {
console.log("ajax call finished");
$(loadbuttonBlogItems).removeClass(\'loading\');
});
})
});