英文:
How to hide the Load More button when all posts are displayed
问题
已实现使用AJAX动态加载帖子。代码有效,帖子正常显示。现在的问题是,当所有帖子都显示后,我需要隐藏按钮,但我不知道如何做到。我该如何获取帖子数量并将其传递给JavaScript以隐藏按钮?如何实现这一点?
let currentPage = 1;
jQuery(document).ready(function($) {
$('#load-more').on('click', function(e) {
e.preventDefault();
currentPage++;
$.ajax({
type: 'POST',
url: '/wp-admin/admin-ajax.php',
dataType: 'json',
data: {
action: 'load_more',
paged: currentPage,
},
success: function(res) {
$('.news-дist').append(res);
}
});
});
});
function load_more() {
$query = new WP_Query([
'post_type' => 'post',
'posts_per_page' => 6,
'orderby' => 'date',
'order' => 'DESC',
'paged' => $_POST['paged'],
]);
$response = '';
if ($query->have_posts()) {
while ($query->have_posts()) : $query->the_post();
$img_url = get_the_post_thumbnail_url($query->post->ID);
$post_title = get_the_title($query->post_title);
$post_author = get_the_author($query->post_author);
$post_date = get_the_date('d F Y');
$post_permalink = get_permalink($query->post->ID);
echo '<li>
<a href="' . $post_permalink . '">
<div class="latest-news__image">
<img src="' . $img_url . '" alt="' . $post_title . '">
</div>
<h4>' . $post_title . '</h4>
<div class="post-credentials">
<p class="post-author">By ' . $post_author . '</p>
<p class="post-date">' . $post_date . '</p>
</div>
</a>
</li> ';
endwhile;
} else {
$response = 'empty';
}
echo $response;
exit;
}
add_action('wp_ajax_load_more', 'load_more');
add_action('wp_ajax_nopriv_load_more', 'load_more');
英文:
Implemented dynamic loading of posts using AJAX. The code works and the posts come as they should. Now the problem is that I need to hide the button when all posts have been displayed, but I don't understand how to do it. How can I get the number of posts and pass that to js to hide the button. How can I implement it?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let currentPage = 1;
jQuery(document).ready(function($) {
$('#load-more').on('click', function(e) {
e.preventDefault();
currentPage++;
$.ajax({
type: 'POST',
url: '/wp-admin/admin-ajax.php',
dataType: 'json',
data: {
action: 'load_more',
paged: currentPage,
},
success: function(res) {
$('.news-дist').append(res);
}
});
});
});
<!-- end snippet -->
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function load_more()
{
$query = new WP_Query([
'post_type' => 'post',
'posts_per_page' => 6,
'orderby' => 'date',
'order' => 'DESC',
'paged' => $_POST['paged'],
]);
$response = '';
if ($query->have_posts()) {
while ($query->have_posts()) : $query->the_post();
$img_url = get_the_post_thumbnail_url($query->post->ID);
$post_title = get_the_title($query->post_title);
$post_author = get_the_author($query->post_author);
$post_date = get_the_date('d F Y');
$post_permalink = get_permalink($query->post->ID);
echo '<li>
<a href="' . $post_permalink . '">
<div class="latest-news__image">
<img src="' . $img_url . '" alt="' . $post_title . '">
</div>
<h4>' . $post_title . '</h4>
<div class="post-credentials">
<p class="post-author">By ' . $post_author . '</p>
<p class="post-date">' . $post_date . '</p>
</div>
</a>
</li> ';
endwhile;
} else {
$response = 'empty';
}
echo $response;
exit;
}
add_action('wp_ajax_load_more', 'load_more');
add_action('wp_ajax_nopriv_load_more', 'load_more');
<!-- end snippet -->
答案1
得分: 1
在Ajax响应中,您需要以JSON格式传递最大页数,然后如果当前页面大于或等于最大页面时,隐藏按钮。
js:
(function ($) {
let currentPage = 1;
$(document).ready(function () {
$("#load-more").on("click", function (e) {
e.preventDefault();
currentPage++;
$.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php",
dataType: "json",
data: {
action: "load_more",
paged: currentPage,
},
success: function (res) {
if (currentPage >= res.max) {
$("#load-more").hide();
}
$(".news-list").append(res.html);
},
});
});
});
})(jQuery);
Ajax回调:
function load_more()
{
$query = new WP_Query([
'post_type' => 'post',
'posts_per_page' => 2,
'orderby' => 'date',
'order' => 'DESC',
'paged' => $_POST['paged'],
]);
$response = '';
$max_pages = $query->max_num_pages;
if ($query->have_posts()) {
while ($query->have_posts()): $query->the_post();
$img_url = get_the_post_thumbnail_url($query->post->ID);
$post_title = get_the_title($query->post_title);
$post_author = get_the_author($query->post_author);
$post_date = get_the_date('d F Y');
$post_permalink = get_permalink($query->post->ID);
$response .= ' <a href="' . $post_permalink . '">
<div class="latest-news__image">
<img src="' . $img_url . '" alt="' . $post_title . '">
</div>
<h4>' . $post_title . '</h4>
<div class="post-credentials">
<p class="post-author">By ' . $post_author . '</p>
<p class="post-date">' . $post_date . '</p>
</div>
</a> ';
endwhile;
} else {
$response = '';
}
$result = [
'max' => $max_pages,
'html' => $response,
];
echo json_encode($result);
exit;
}
add_action('wp_ajax_load_more', 'load_more');
add_action('wp_ajax_nopriv_load_more', 'load_more');
如您所要求的,这是翻译好的内容,没有其他信息。
英文:
In the ajax response, you need to pass the max pages in JSON format. Then hide the button if the current page is greater or equal to the max page.
js:
(function ($) {
let currentPage = 1;
$(document).ready(function () {
$("#load-more").on("click", function (e) {
e.preventDefault();
currentPage++;
$.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php",
dataType: "json",
data: {
action: "load_more",
paged: currentPage,
},
success: function (res) {
if (currentPage >= res.max) {
$("#load-more").hide();
}
$(".news-list").append(res.html);
},
});
});
});
})(jQuery);
Ajax callback:
function load_more()
{
$query = new WP_Query([
'post_type' => 'post',
'posts_per_page' => 2,
'orderby' => 'date',
'order' => 'DESC',
'paged' => $_POST['paged'],
]);
$response = '';
$max_pages = $query->max_num_pages;
if ($query->have_posts()) {
while ($query->have_posts()): $query->the_post();
$img_url = get_the_post_thumbnail_url($query->post->ID);
$post_title = get_the_title($query->post_title);
$post_author = get_the_author($query->post_author);
$post_date = get_the_date('d F Y');
$post_permalink = get_permalink($query->post->ID);
$response .= ' <a href="' . $post_permalink . '">
<div class="latest-news__image">
<img src="' . $img_url . '" alt="' . $post_title . '">
</div>
<h4>' . $post_title . '</h4>
<div class="post-credentials">
<p class="post-author">By ' . $post_author . '</p>
<p class="post-date">' . $post_date . '</p>
</div>
</a> ';
endwhile;
} else {
$response = '';
}
$result = [
'max' => $max_pages,
'html' => $response,
];
echo json_encode($result);
exit;
}
add_action('wp_ajax_load_more', 'load_more');
add_action('wp_ajax_nopriv_load_more', 'load_more');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论