如何在显示所有帖子时隐藏“加载更多”按钮

huangapple go评论153阅读模式
英文:

How to hide the Load More button when all posts are displayed

问题

已实现使用AJAX动态加载帖子。代码有效,帖子正常显示。现在的问题是,当所有帖子都显示后,我需要隐藏按钮,但我不知道如何做到。我该如何获取帖子数量并将其传递给JavaScript以隐藏按钮?如何实现这一点?

  1. let currentPage = 1;
  2. jQuery(document).ready(function($) {
  3. $('#load-more').on('click', function(e) {
  4. e.preventDefault();
  5. currentPage++;
  6. $.ajax({
  7. type: 'POST',
  8. url: '/wp-admin/admin-ajax.php',
  9. dataType: 'json',
  10. data: {
  11. action: 'load_more',
  12. paged: currentPage,
  13. },
  14. success: function(res) {
  15. $('.news-дist').append(res);
  16. }
  17. });
  18. });
  19. });
  1. function load_more() {
  2. $query = new WP_Query([
  3. 'post_type' => 'post',
  4. 'posts_per_page' => 6,
  5. 'orderby' => 'date',
  6. 'order' => 'DESC',
  7. 'paged' => $_POST['paged'],
  8. ]);
  9. $response = '';
  10. if ($query->have_posts()) {
  11. while ($query->have_posts()) : $query->the_post();
  12. $img_url = get_the_post_thumbnail_url($query->post->ID);
  13. $post_title = get_the_title($query->post_title);
  14. $post_author = get_the_author($query->post_author);
  15. $post_date = get_the_date('d F Y');
  16. $post_permalink = get_permalink($query->post->ID);
  17. echo '<li>
  18. <a href="' . $post_permalink . '">
  19. <div class="latest-news__image">
  20. <img src="' . $img_url . '" alt="' . $post_title . '">
  21. </div>
  22. <h4>' . $post_title . '</h4>
  23. <div class="post-credentials">
  24. <p class="post-author">By ' . $post_author . '</p>
  25. <p class="post-date">' . $post_date . '</p>
  26. </div>
  27. </a>
  28. </li> ';
  29. endwhile;
  30. } else {
  31. $response = 'empty';
  32. }
  33. echo $response;
  34. exit;
  35. }
  36. add_action('wp_ajax_load_more', 'load_more');
  37. 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 -->

  1. let currentPage = 1;
  2. jQuery(document).ready(function($) {
  3. $(&#39;#load-more&#39;).on(&#39;click&#39;, function(e) {
  4. e.preventDefault();
  5. currentPage++;
  6. $.ajax({
  7. type: &#39;POST&#39;,
  8. url: &#39;/wp-admin/admin-ajax.php&#39;,
  9. dataType: &#39;json&#39;,
  10. data: {
  11. action: &#39;load_more&#39;,
  12. paged: currentPage,
  13. },
  14. success: function(res) {
  15. $(&#39;.newsist&#39;).append(res);
  16. }
  17. });
  18. });
  19. });

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. function load_more()
  2. {
  3. $query = new WP_Query([
  4. &#39;post_type&#39; =&gt; &#39;post&#39;,
  5. &#39;posts_per_page&#39; =&gt; 6,
  6. &#39;orderby&#39; =&gt; &#39;date&#39;,
  7. &#39;order&#39; =&gt; &#39;DESC&#39;,
  8. &#39;paged&#39; =&gt; $_POST[&#39;paged&#39;],
  9. ]);
  10. $response = &#39;&#39;;
  11. if ($query-&gt;have_posts()) {
  12. while ($query-&gt;have_posts()) : $query-&gt;the_post();
  13. $img_url = get_the_post_thumbnail_url($query-&gt;post-&gt;ID);
  14. $post_title = get_the_title($query-&gt;post_title);
  15. $post_author = get_the_author($query-&gt;post_author);
  16. $post_date = get_the_date(&#39;d F Y&#39;);
  17. $post_permalink = get_permalink($query-&gt;post-&gt;ID);
  18. echo &#39;&lt;li&gt;
  19. &lt;a href=&quot;&#39; . $post_permalink . &#39;&quot;&gt;
  20. &lt;div class=&quot;latest-news__image&quot;&gt;
  21. &lt;img src=&quot;&#39; . $img_url . &#39;&quot; alt=&quot;&#39; . $post_title . &#39;&quot;&gt;
  22. &lt;/div&gt;
  23. &lt;h4&gt;&#39; . $post_title . &#39;&lt;/h4&gt;
  24. &lt;div class=&quot;post-credentials&quot;&gt;
  25. &lt;p class=&quot;post-author&quot;&gt;By &#39; . $post_author . &#39;&lt;/p&gt;
  26. &lt;p class=&quot;post-date&quot;&gt;&#39; . $post_date . &#39;&lt;/p&gt;
  27. &lt;/div&gt;
  28. &lt;/a&gt;
  29. &lt;/li&gt; &#39;;
  30. endwhile;
  31. } else {
  32. $response = &#39;empty&#39;;
  33. }
  34. echo $response;
  35. exit;
  36. }
  37. add_action(&#39;wp_ajax_load_more&#39;, &#39;load_more&#39;);
  38. add_action(&#39;wp_ajax_nopriv_load_more&#39;, &#39;load_more&#39;);

<!-- end snippet -->

答案1

得分: 1

在Ajax响应中,您需要以JSON格式传递最大页数,然后如果当前页面大于或等于最大页面时,隐藏按钮。

js:

  1. (function ($) {
  2. let currentPage = 1;
  3. $(document).ready(function () {
  4. $("#load-more").on("click", function (e) {
  5. e.preventDefault();
  6. currentPage++;
  7. $.ajax({
  8. type: "POST",
  9. url: "/wp-admin/admin-ajax.php",
  10. dataType: "json",
  11. data: {
  12. action: "load_more",
  13. paged: currentPage,
  14. },
  15. success: function (res) {
  16. if (currentPage >= res.max) {
  17. $("#load-more").hide();
  18. }
  19. $(".news-list").append(res.html);
  20. },
  21. });
  22. });
  23. });
  24. })(jQuery);

Ajax回调:

  1. function load_more()
  2. {
  3. $query = new WP_Query([
  4. 'post_type' => 'post',
  5. 'posts_per_page' => 2,
  6. 'orderby' => 'date',
  7. 'order' => 'DESC',
  8. 'paged' => $_POST['paged'],
  9. ]);
  10. $response = '';
  11. $max_pages = $query->max_num_pages;
  12. if ($query->have_posts()) {
  13. while ($query->have_posts()): $query->the_post();
  14. $img_url = get_the_post_thumbnail_url($query->post->ID);
  15. $post_title = get_the_title($query->post_title);
  16. $post_author = get_the_author($query->post_author);
  17. $post_date = get_the_date('d F Y');
  18. $post_permalink = get_permalink($query->post->ID);
  19. $response .= ' <a href="' . $post_permalink . '">
  20. <div class="latest-news__image">
  21. <img src="' . $img_url . '" alt="' . $post_title . '">
  22. </div>
  23. <h4>' . $post_title . '</h4>
  24. <div class="post-credentials">
  25. <p class="post-author">By ' . $post_author . '</p>
  26. <p class="post-date">' . $post_date . '</p>
  27. </div>
  28. </a> ';
  29. endwhile;
  30. } else {
  31. $response = '';
  32. }
  33. $result = [
  34. 'max' => $max_pages,
  35. 'html' => $response,
  36. ];
  37. echo json_encode($result);
  38. exit;
  39. }
  40. add_action('wp_ajax_load_more', 'load_more');
  41. 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:

  1. (function ($) {
  2. let currentPage = 1;
  3. $(document).ready(function () {
  4. $(&quot;#load-more&quot;).on(&quot;click&quot;, function (e) {
  5. e.preventDefault();
  6. currentPage++;
  7. $.ajax({
  8. type: &quot;POST&quot;,
  9. url: &quot;/wp-admin/admin-ajax.php&quot;,
  10. dataType: &quot;json&quot;,
  11. data: {
  12. action: &quot;load_more&quot;,
  13. paged: currentPage,
  14. },
  15. success: function (res) {
  16. if (currentPage &gt;= res.max) {
  17. $(&quot;#load-more&quot;).hide();
  18. }
  19. $(&quot;.news-list&quot;).append(res.html);
  20. },
  21. });
  22. });
  23. });
  24. })(jQuery);

Ajax callback:

  1. function load_more()
  2. {
  3. $query = new WP_Query([
  4. &#39;post_type&#39; =&gt; &#39;post&#39;,
  5. &#39;posts_per_page&#39; =&gt; 2,
  6. &#39;orderby&#39; =&gt; &#39;date&#39;,
  7. &#39;order&#39; =&gt; &#39;DESC&#39;,
  8. &#39;paged&#39; =&gt; $_POST[&#39;paged&#39;],
  9. ]);
  10. $response = &#39;&#39;;
  11. $max_pages = $query-&gt;max_num_pages;
  12. if ($query-&gt;have_posts()) {
  13. while ($query-&gt;have_posts()): $query-&gt;the_post();
  14. $img_url = get_the_post_thumbnail_url($query-&gt;post-&gt;ID);
  15. $post_title = get_the_title($query-&gt;post_title);
  16. $post_author = get_the_author($query-&gt;post_author);
  17. $post_date = get_the_date(&#39;d F Y&#39;);
  18. $post_permalink = get_permalink($query-&gt;post-&gt;ID);
  19. $response .= &#39; &lt;a href=&quot;&#39; . $post_permalink . &#39;&quot;&gt;
  20. &lt;div class=&quot;latest-news__image&quot;&gt;
  21. &lt;img src=&quot;&#39; . $img_url . &#39;&quot; alt=&quot;&#39; . $post_title . &#39;&quot;&gt;
  22. &lt;/div&gt;
  23. &lt;h4&gt;&#39; . $post_title . &#39;&lt;/h4&gt;
  24. &lt;div class=&quot;post-credentials&quot;&gt;
  25. &lt;p class=&quot;post-author&quot;&gt;By &#39; . $post_author . &#39;&lt;/p&gt;
  26. &lt;p class=&quot;post-date&quot;&gt;&#39; . $post_date . &#39;&lt;/p&gt;
  27. &lt;/div&gt;
  28. &lt;/a&gt; &#39;;
  29. endwhile;
  30. } else {
  31. $response = &#39;&#39;;
  32. }
  33. $result = [
  34. &#39;max&#39; =&gt; $max_pages,
  35. &#39;html&#39; =&gt; $response,
  36. ];
  37. echo json_encode($result);
  38. exit;
  39. }
  40. add_action(&#39;wp_ajax_load_more&#39;, &#39;load_more&#39;);
  41. add_action(&#39;wp_ajax_nopriv_load_more&#39;, &#39;load_more&#39;);

huangapple
  • 本文由 发表于 2023年2月27日 03:24:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定