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

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

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($) {
  $(&#39;#load-more&#39;).on(&#39;click&#39;, function(e) {
    e.preventDefault();
    currentPage++;
    $.ajax({
      type: &#39;POST&#39;,
      url: &#39;/wp-admin/admin-ajax.php&#39;,
      dataType: &#39;json&#39;,
      data: {
        action: &#39;load_more&#39;,
        paged: currentPage,
      },
      success: function(res) {
        $(&#39;.news-дist&#39;).append(res);
      }
    });
  });
});

<!-- end snippet -->

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

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

function load_more()
{
    $query = new WP_Query([
        &#39;post_type&#39; =&gt; &#39;post&#39;,
        &#39;posts_per_page&#39; =&gt; 6,
        &#39;orderby&#39; =&gt; &#39;date&#39;,
        &#39;order&#39; =&gt; &#39;DESC&#39;,
        &#39;paged&#39; =&gt; $_POST[&#39;paged&#39;],
    ]);

    $response = &#39;&#39;;

    if ($query-&gt;have_posts()) {
        while ($query-&gt;have_posts()) : $query-&gt;the_post();
            $img_url = get_the_post_thumbnail_url($query-&gt;post-&gt;ID);
            $post_title = get_the_title($query-&gt;post_title);
            $post_author = get_the_author($query-&gt;post_author);
            $post_date = get_the_date(&#39;d F Y&#39;);
            $post_permalink = get_permalink($query-&gt;post-&gt;ID);

            echo &#39;&lt;li&gt;
	          &lt;a href=&quot;&#39; . $post_permalink . &#39;&quot;&gt;
	            &lt;div class=&quot;latest-news__image&quot;&gt;
                &lt;img src=&quot;&#39; . $img_url . &#39;&quot; alt=&quot;&#39; . $post_title . &#39;&quot;&gt;
                &lt;/div&gt;
                &lt;h4&gt;&#39; . $post_title . &#39;&lt;/h4&gt;
                 &lt;div class=&quot;post-credentials&quot;&gt;
                 &lt;p class=&quot;post-author&quot;&gt;By &#39; . $post_author . &#39;&lt;/p&gt;
                 &lt;p class=&quot;post-date&quot;&gt;&#39; . $post_date . &#39;&lt;/p&gt;
                  &lt;/div&gt;
	           &lt;/a&gt;
		    &lt;/li&gt; &#39;;
        endwhile;
    } else {
        $response = &#39;empty&#39;;
    }

    echo $response;
    exit;
}

add_action(&#39;wp_ajax_load_more&#39;, &#39;load_more&#39;);
add_action(&#39;wp_ajax_nopriv_load_more&#39;, &#39;load_more&#39;);

<!-- 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 () {
    $(&quot;#load-more&quot;).on(&quot;click&quot;, function (e) {
      e.preventDefault();
      currentPage++;

      $.ajax({
        type: &quot;POST&quot;,
        url: &quot;/wp-admin/admin-ajax.php&quot;,
        dataType: &quot;json&quot;,
        data: {
          action: &quot;load_more&quot;,
          paged: currentPage,
        },
        success: function (res) {
          if (currentPage &gt;= res.max) {
            $(&quot;#load-more&quot;).hide();
          }
          $(&quot;.news-list&quot;).append(res.html);
        },
      });
    });
  });
})(jQuery);

Ajax callback:

function load_more()
{
    $query = new WP_Query([
        &#39;post_type&#39;      =&gt; &#39;post&#39;,
        &#39;posts_per_page&#39; =&gt; 2,
        &#39;orderby&#39;        =&gt; &#39;date&#39;,
        &#39;order&#39;          =&gt; &#39;DESC&#39;,
        &#39;paged&#39;          =&gt; $_POST[&#39;paged&#39;],
    ]);

    $response  = &#39;&#39;;
    $max_pages = $query-&gt;max_num_pages;

    if ($query-&gt;have_posts()) {
        while ($query-&gt;have_posts()): $query-&gt;the_post();
            $img_url        = get_the_post_thumbnail_url($query-&gt;post-&gt;ID);
            $post_title     = get_the_title($query-&gt;post_title);
            $post_author    = get_the_author($query-&gt;post_author);
            $post_date      = get_the_date(&#39;d F Y&#39;);
            $post_permalink = get_permalink($query-&gt;post-&gt;ID);

            $response .= &#39; &lt;a href=&quot;&#39; . $post_permalink . &#39;&quot;&gt;
                              &lt;div class=&quot;latest-news__image&quot;&gt;
                              &lt;img src=&quot;&#39; . $img_url . &#39;&quot; alt=&quot;&#39; . $post_title . &#39;&quot;&gt;
                              &lt;/div&gt;
                              &lt;h4&gt;&#39; . $post_title . &#39;&lt;/h4&gt;
                                &lt;div class=&quot;post-credentials&quot;&gt;
                                &lt;p class=&quot;post-author&quot;&gt;By &#39; . $post_author . &#39;&lt;/p&gt;
                                &lt;p class=&quot;post-date&quot;&gt;&#39; . $post_date . &#39;&lt;/p&gt;
                                &lt;/div&gt;
                          &lt;/a&gt; &#39;;
        endwhile;
    } else {
        $response = &#39;&#39;;
    }

    $result = [
        &#39;max&#39;  =&gt; $max_pages,
        &#39;html&#39; =&gt; $response,
    ];

    echo json_encode($result);
    exit;
}
add_action(&#39;wp_ajax_load_more&#39;, &#39;load_more&#39;);
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:

确定