我使用AJAX来显示更多"。目前我的自定义帖子类型显示最初是12,当"显示更多"单击按钮,在页面上加载了12个post-types
。
问题:首次点击后,会显示12。但每次点击都会继续,它会继续重复之前填充的12个项目。
问题:当用户点击"显示更多"。
时,如何正确使用AJAX来显示12秒的帖子?Functions.php中的AJAX处理程序
function ajax_more_team($offset) {
$offset = $offset + 12;
header("Content-Type: text/html");
$args = array(
'suppress_filters' => true,
'post_type' => 'team',
'posts_per_page' => 12,
'offset' => $offset,
);
$the_query = new WP_Query($args);
if ($the_query -> have_posts()) : while ($the_query -> have_posts()) : $the_query -> the_post();
//Loop content
endwhile;
endif;
wp_reset_postdata();
die($out);
}
Jquery功能
var count = 0;
function load_more_team(count) {
var count = count + 12
var button = $('#more_posts'),
data = {
'action': 'ajax_more_team',
'offset': count
}
$.ajax({
url: team_ajax.ajaxurl,
data: data,
type: 'POST',
dataType: 'html',
success: function(data){
if(data.length){
$("#ajax_posts").append(data);
button.attr("disabled",false);
} else{
button.attr("disabled",true);
}
}
});
return false;
}
$('#more_posts').click(function() {
$("#more_posts").attr("disabled",true); // Disable the button, temp.
load_more_team();
});
page.php文件
<div id="ajax_posts" class="row">
<?php
$args = array(
'post_type' => 'team',
'posts_per_page' => 12
);
$the_query = new WP_Query($args);
?>
<?php if($the_query->have_posts()) : while($the_query->have_posts()) : $the_query->the_post(); ?>
<?php $id = get_the_id(); ?>
<div class="col-6 col-md-4 col-lg-3 col-xl-2 mb-4">
<div class="team-member">
<a href="#" data-toggle="modal" data-target="#<?php echo $id ?>">
<img class="img-fluid" src="<?php the_field('employee_headshot'); ?>" alt="<?php the_field('employee_name'); ?>">
</a>
<div class="team-info">
<h6><?php the_field('employee_name'); ?></h6>
</div>
<a href="" data-toggle="modal" data-target="#myModal">
<div class="modal-icon">
<img class="img-fluid" src="<?php bloginfo('template_directory');?>/imgs/modal-icon.svg">
</div>
</a>
</div>
<!-- Modal -->
<div class="modal fade" id="<?php echo $id ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="team-close-btn">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<img class="img-fluid" src="<?php the_field('employee_headshot'); ?>" alt="Alice George">
<div class="team-info">
<h6><?php the_field('employee_name'); ?></h6>
<p><strong>Title:<br></strong> <?php the_field('employee_title'); ?></p>
<p><strong>Company:<br></strong> <?php the_field('employee_company'); ?></p>
</div>
</div>
</div>
</div>
</div>
</div>
<?php endwhile; endif; ?>
</div>
<?php
if( $the_query->max_num_pages > 1)
echo '<div id="more_posts" class="btn-primary mt-4">View More</div>'
?>
<?php wp_reset_postdata(); ?>
</div>
答案 0 :(得分:1)
已更新为您的代码,这应该适合您。我认为计数有点令人困惑,因为对我来说,如果它是一个总金额或当前页面,它是不是很清楚。
不确定你如何在PHP函数中收到$ offset值,但这里的$ offset应该只是POST值&#39; offset&#39;,no + 12或者其他任何内容。这种方式在第一次调用时你的偏移量为0,从第1行开始获得前12条记录,下次你有12的偏移并获得接下来的12条记录(从第13行开始)。您的post_per_page保持为12,而偏移量则相乘。偏移量表示应该从哪些记录开始采用&#34; post_per_page&#34;量。
function ajax_more_team($offset) {
$offset = $_POST['offset'];
header("Content-Type: text/html");
$args = array(
'suppress_filters' => true,
'post_type' => 'team',
'posts_per_page' => 12,
'offset' => $offset,
);
$the_query = new WP_Query($args);
if ($the_query -> have_posts()) : while ($the_query -> have_posts()) : $the_query -> the_post();
//Loop content
endwhile;
endif;
wp_reset_postdata();
die($out);
}
-
var page = 1; // first page
function load_more_team() {
var button = $('#more_posts'),
data = {
'action': 'ajax_more_team',
'offset': page * 12 // first time 0 * 12 = offset 0
}
$.ajax({
url: team_ajax.ajaxurl,
data: data,
type: 'POST',
dataType: 'html',
success: function(data){
if(data.length){
$("#ajax_posts").append(data);
button.attr("disabled",false);
} else{
button.attr("disabled",true);
}
page++; // increment page after first request
}
});
return false;
}
答案 1 :(得分:0)
var page = 2; // first page already loaded
function load_more_team() {
var button = $('#more_posts'),
data = {
'action': 'ajax_more_team',
'paged': page
}
$.ajax({
url: team_ajax.ajaxurl,
data: data,
type: 'POST',
dataType: 'html',
success: function(data){
if(data.length){
$("#ajax_posts").append(data);
button.attr("disabled",false);
} else{
button.attr("disabled",true);
}
page++; // increment page after request
}
});
return false;
}
function ajax_more_team() {
$paged = $_POST['paged'];
header("Content-Type: text/html");
$args = array(
'suppress_filters' => true,
'post_type' => 'team',
'post_status' => 'publish',
'posts_per_page' => 12,
'paged' => $paged,
);
$the_query = new WP_Query($args);
if ($the_query -> have_posts()) : while ($the_query -> have_posts()) : $the_query -> the_post();
//Loop content
endwhile;
endif;
wp_reset_postdata();
die($out);
}