我正在尝试使用ajax对帖子进行分页。
我将一个属性“data-page”设置为页面链接,并使用jQuery将其解压缩,以便用Ajax传递它。
第一次点击链接一切正常,但如果我点击另一个链接,ajax调用返回0。
这是我的functions.php中的代码:
wp_register_script('load_post_ajax', get_template_directory_uri() . '/includes/ajax/load_post_ajax.js',array( 'jquery' ), 1.1, true);
$php_array = array( 'admin_ajax' => admin_url( 'admin-ajax.php' ) );
wp_localize_script( 'load_post_ajax', 'php_array', $php_array );
wp_enqueue_script( 'load_post_ajax' );
/ * * PAGINATION * /
function hs_pagination($pages = '', $range = 4)
{
$showitems = ($range * 2)+1;
if(isset($_POST['paged'])){
$paged = $_POST['paged'];
}else{
$paged = get_query_var( 'paged', 1 );
}
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages." </span>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."' data-page=1>« First</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹ Previous</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<a href=\"#\" class=\"current\" data-page=$i>".$i."</a>":"<a href='".get_pagenum_link($i)."' class=\"inactive\" data-page=$i>".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\" data-page=$paged + 1>Next ›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."' data-page=$pages>Last »</a>";
echo "</div>\n";
}
}
/ * * AJAX POSTS LOADER * /
add_action( 'wp_ajax_load_post_ajax', 'load_post_ajax_init' );
add_action( 'wp_ajax_nopriv_load_post_ajax', 'load_post_ajax_init' );
function load_post_ajax_init() {
$total_posts_per_page = get_option('posts_per_page');
if($_POST['paged']){
$paged = $_POST['paged'];
}else{
$paged = get_query_var( 'paged', 1 );
}
$args = array(
'posts_per_page' => $total_posts_per_page,
'paged' => $paged,
'post_type' => 'post'
);
$posts = query_posts($args);
if($posts){
$count =0;
foreach($posts as $post){
?>
<article class="post">
<?php
if($paged < 2){
if(has_post_thumbnail($post->ID) ){
the_post_thumbnail($post->ID,'big',array('class'=>'img-responsive center-block'));
}else{
echo "no thumb";
}
}else{
echo "not first post";
}
?>
<p class="post_info hs_color"><?php echo $post->post_title . " Posted on: " . $post->post_date . " Author: ". get_the_author_meta( 'display_name',$post->post_author );?></p>
<p><?php echo $post->post_content;?></p>
<div class="post_link clearfix">
<a href="<?php the_permalink($post->ID);?>">
<h6 class="text-uppercase text-center">Read more</h6>
<div class="hs_square hs_square_right hs_bg"></div>
</a>
</div>
</article>
<?php
$count++;
}
}else {
echo "no posts found";
}
//PAGINATION
hs_pagination();
//PAGINATION
}
/ * * AJAx CALL * /
jQuery( document ).ready(function() {
$('#post-section .container .pagination a').on('click',function(e){
/** Prevent Default Behaviour */
e.preventDefault();
var page = $(this).attr('data-page');
/** Ajax Call */
$.ajax({
cache: false,
timeout: 8000,
url: php_array.admin_ajax,
type: "POST",
data: ({
action:'load_post_ajax',
paged:page,
}),
beforeSend: function() {
},
success: function( data,response ){
$( '#post-section .container' ).addClass('animated FadeIn');
$( '#post-section .container' ).html( data);
},
error: function( jqXHR, textStatus, errorThrown ){
console.log( 'The following error occured: ' + textStatus, errorThrown );
},
});
});
});