我在page.php底部加载了更多按钮。不幸的是,尽管在ajax上取得了成功,但它并没有加载任何东西。
代码如下:
myloadmore.js
jQuery(function($){
$('.load-articles').on('click', function() {
var self = $(this),
page = self.data('page'),
newPage = page + 1,
ajaxurl = self.data('url'),
post_types = self.data('types'),
post_template = self.data('tmpl');
$.ajax({
url : ajaxurl,
type : 'post',
data : {
page : page,
types : post_types,
action : 'more_articles_ajax',
tmpl : post_template
},
beforeSend : function ( xhr ) {
$('.load-articles').text('Ładuję...'); // change the button text, you can also add a preloader image
},
error : function( response ) {
console.log( response );
},
success : function( response ) {
console.log(response);
if (response) {
self.data('page', newPage);
$('.loadmore-container').hide().append(response).fadeIn(500);
$('.load-articles').text('zobacz więcej');
} else {
$('.load-articles').remove();
}
}
});
});
});
我在functions.php中的功能
function more_articles_ajax() {
$paged = $_POST['page'] + 1;
$types = $_POST['types'];
$tmpl = $_POST['tmpl'];
$query = new WP_Query( array(
'post_type' => array($types),
'posts_per_page' => 6,
'paged' => $paged,
'order' => 'ASC',
) );
if( $query->have_posts() ):
while( $query->have_posts() ): $query->the_post();
get_template_part( 'template-parts/content', $tmpl);
endwhile;
endif;
wp_reset_postdata();
die();
}
add_action('wp_ajax_nopriv_more_articles_ajax', 'more_articles_ajax');
add_action('wp_ajax_more_articles_ajax', 'more_articles_ajax');
和页面内容:
<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'numberposts' => 999,
'post_type' => array('news'),
'order' => 'DESC',
'posts_per_page' => 2,
'paged' => get_query_var('page'),
'page' => $paged
);
$latest_services = new WP_Query ( $args );
$i = 0;
?>
<?php if ($latest_services->have_posts()): while ($latest_services->have_posts()) : $latest_services->the_post();?>
//some posts
<?php endwhile; ?>
<div class="loadmore-container"></div>
<div class="loadmore column load-articles" data-page="1" data-types="news" data-tmpl="article-item" data-url="<?php echo admin_url('admin-ajax.php'); ?>">see more</div>
<?php else: ?>
<?php endif; wp_reset_postdata(); ?>
编辑: 好的,我只需要补充一下,当输入www.home-url / page / 2 /我可以看到我的内容被分页。但是,当我添加我的分页模板时,它不显示任何内容(但它在其他页面上运行良好)。