我正在尝试在wordpress上创建ajax分页。 我需要做的是最初显示6个帖子,然后在点击“加载更多帖子”链接时再加载3个帖子 这是我的代码,但只加载一个
功能:
function more_news_ajax(){
$offset = $_POST["offset"];
$ppp = $_POST["ppp"];
header("Content-Type: text/html");
$args3 = array(
'posts_per_page' => $ppp,
'offset' => $offset,
'post_type' => 'post',
);
$custom3 = new WP_Query($args3);
while ($custom3->have_posts()) : $custom3->the_post();
echo '<div class="blog-loop-article" id="evento-';echo get_the_ID().'">
<div class="meta">
<p class="data"><strong>';the_time('j F Y');echo '</strong></p>
<h3>';the_title();echo '</h3>
<a href="'; the_permalink(); echo '" rel="bookmark"> <p><strong>read more</strong></p></a>
</div>';
if(has_post_thumbnail()) :
echo'
<div class="abstract">'; echo custom_trim_excerpt(30).'</div>
<div class="thumb">';
the_post_thumbnail('thumb-blog', array('class' => 'img-responsive')).'
</div>';
else :
'
<div class="abstract large">'; echo custom_trim_excerpt(30);'</div>';
endif;'
</div>';
endwhile;
exit;
}
add_action('wp_ajax_nopriv_more_news_ajax', 'more_news_ajax');
add_action('wp_ajax_more_news_ajax', 'more_news_ajax');
脚本
<script type="text/javascript">
var ajaxUrl = "<?php echo admin_url('admin-ajax.php', null); ?>";
var page = 1; // What page we are on.
var ppp = 3; // Post per page
jQuery("#morenews").on("click",function(){ // When btn is pressed.
jQuery("#morenews").attr("disabled",true); // Disable the button, temp.
jQuery.post(ajaxUrl, {
action:"more_news_ajax",
offset: (page * ppp) + 1,
ppp: ppp
}).success(function(posts){
page++;
jQuery("#listnews").append(posts);
jQuery("#morenews").attr("disabled",false);
});
});
</script>
page.php文件
<?php
$args = array(
'post_type' => 'post',
'order' => 'DESC',
'posts_per_page' => '-1',
'paged' => get_query_var( 'paged' )
);
$showpost = new WP_Query( $args);
if ($showpost->have_posts()) ;
?>
<?php query_posts($args); while ($showpost->have_posts()): $showpost->the_post(); ?>
<div class="blog-loop-article" id="evento-<?php echo get_the_ID() ?>">
<div class="meta">
<p class="data"><?php the_time('j F Y') ?></p>
<h3><?php the_title(); ?></h3>
<a class="morepress" href="<?php the_permalink(); ?>" rel="bookmark"> <p>read more</p></a>
</div>
<?php
if(has_post_thumbnail()) :
?>
<div class="abstract"><?php echo custom_trim_excerpt(30); ?></div>
<div class="thumb">
<?php the_post_thumbnail('thumb-blog', array('class' => 'img-responsive')); ?>
</div>
<?php else :?>
<div class="abstract large"><?php echo custom_trim_excerpt(30); ?></div>
<?php endif;?>
</a>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
<div class="pagination">
<div id="morenews" class="loadmore news">LOAD MORE</div>
</div>