<tfoot id='4FDp0'></tfoot>
        <bdo id='4FDp0'></bdo><ul id='4FDp0'></ul>
    1. <i id='4FDp0'><tr id='4FDp0'><dt id='4FDp0'><q id='4FDp0'><span id='4FDp0'><b id='4FDp0'><form id='4FDp0'><ins id='4FDp0'></ins><ul id='4FDp0'></ul><sub id='4FDp0'></sub></form><legend id='4FDp0'></legend><bdo id='4FDp0'><pre id='4FDp0'><center id='4FDp0'></center></pre></bdo></b><th id='4FDp0'></th></span></q></dt></tr></i><div id='4FDp0'><tfoot id='4FDp0'></tfoot><dl id='4FDp0'><fieldset id='4FDp0'></fieldset></dl></div>
      1. <small id='4FDp0'></small><noframes id='4FDp0'>

        <legend id='4FDp0'><style id='4FDp0'><dir id='4FDp0'><q id='4FDp0'></q></dir></style></legend>

      2. 如何在自定义 WP_Query Ajax 上实现分页

        How to implement pagination on a custom WP_Query Ajax(如何在自定义 WP_Query Ajax 上实现分页)
        • <bdo id='lTXLZ'></bdo><ul id='lTXLZ'></ul>

          <i id='lTXLZ'><tr id='lTXLZ'><dt id='lTXLZ'><q id='lTXLZ'><span id='lTXLZ'><b id='lTXLZ'><form id='lTXLZ'><ins id='lTXLZ'></ins><ul id='lTXLZ'></ul><sub id='lTXLZ'></sub></form><legend id='lTXLZ'></legend><bdo id='lTXLZ'><pre id='lTXLZ'><center id='lTXLZ'></center></pre></bdo></b><th id='lTXLZ'></th></span></q></dt></tr></i><div id='lTXLZ'><tfoot id='lTXLZ'></tfoot><dl id='lTXLZ'><fieldset id='lTXLZ'></fieldset></dl></div>
          <legend id='lTXLZ'><style id='lTXLZ'><dir id='lTXLZ'><q id='lTXLZ'></q></dir></style></legend>
                <tbody id='lTXLZ'></tbody>

                  <tfoot id='lTXLZ'></tfoot>
                • <small id='lTXLZ'></small><noframes id='lTXLZ'>

                  本文介绍了如何在自定义 WP_Query Ajax 上实现分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想使用 Ajax 在自定义循环中对我的 WordPress 帖子进行分页,因此当我点击加载更多按钮时,会出现帖子.

                  I want to paginate my WordPress posts in a custom loop with Ajax, so when I click on load more button posts will appear.

                  我的代码:

                  <?php 
                      $postsPerPage = 3;
                      $args = array(
                          'post_type' => 'post',
                          'posts_per_page' => $postsPerPage,
                          'cat' => 1
                      );
                      $loop = new WP_Query($args);
                  
                      while ($loop->have_posts()) : $loop->the_post();
                  ?>
                  <h1><?php the_title(); ?></h1>
                  <p>
                      <?php the_content(); ?>
                  </p>
                  <?php
                      endwhile; 
                      echo '<a href="#">Load More</a>';
                      wp_reset_postdata(); 
                  ?>
                  

                  此代码不分页.有没有更好的方法来做到这一点?

                  This code does not paginate. Is there a better way to do this?

                  推荐答案

                  Load More按钮需要向服务器发送ajax请求,返回的数据可以使用 jQuery 或普通 javascript 添加到现有内容中.假设您使用 jQuery,这将是入门代码.

                  The Load More button needs to send a ajax request to the server and the returned data can be added to the existent content using jQuery or plain javascript. Assuming your using jQuery this would starter code.

                  自定义 Ajax 处理程序(客户端)

                  <a href="#">Load More</a>
                  

                  更改为:

                  <a id="more_posts" href="#">Load More</a>
                  

                  Javascript: - 把它放在文件的底部.

                  Javascript: - Put this at the bottom of the file.

                  //</script type="text/javascript">
                  
                      var ajaxUrl = "<?php echo admin_url('admin-ajax.php')?>";
                      var page = 1; // What page we are on.
                      var ppp = 3; // Post per page
                  
                      $("#more_posts").on("click",function(){ // When btn is pressed.
                          $("#more_posts").attr("disabled",true); // Disable the button, temp.
                          $.post(ajaxUrl, {
                              action:"more_post_ajax",
                              offset: (page * ppp) + 1,
                              ppp: ppp
                          }).success(function(posts){
                              page++;
                              $(".name_of_posts_class").append(posts); // CHANGE THIS!
                              $("#more_posts").attr("disabled",false);
                          });
                  
                     });
                  
                  //</script>
                  

                  自定义 Ajax 处理程序(服务器端)PHP - 把它放在functions.php 文件中.

                  Custom Ajax Handler (Server-side) PHP - Put this in the functions.php file.

                  function more_post_ajax(){
                      $offset = $_POST["offset"];
                      $ppp = $_POST["ppp"];
                      header("Content-Type: text/html");
                  
                      $args = array(
                          'post_type' => 'post',
                          'posts_per_page' => $ppp,
                          'cat' => 1,
                          'offset' => $offset,
                      );
                  
                      $loop = new WP_Query($args);
                      while ($loop->have_posts()) { $loop->the_post(); 
                         the_content();
                      }
                  
                      exit; 
                  }
                  
                  add_action('wp_ajax_nopriv_more_post_ajax', 'more_post_ajax'); 
                  add_action('wp_ajax_more_post_ajax', 'more_post_ajax');
                  

                  这篇关于如何在自定义 WP_Query Ajax 上实现分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                        <bdo id='2NC5r'></bdo><ul id='2NC5r'></ul>
                      • <small id='2NC5r'></small><noframes id='2NC5r'>

                          <tfoot id='2NC5r'></tfoot>

                              <tbody id='2NC5r'></tbody>
                            <legend id='2NC5r'><style id='2NC5r'><dir id='2NC5r'><q id='2NC5r'></q></dir></style></legend>
                            <i id='2NC5r'><tr id='2NC5r'><dt id='2NC5r'><q id='2NC5r'><span id='2NC5r'><b id='2NC5r'><form id='2NC5r'><ins id='2NC5r'></ins><ul id='2NC5r'></ul><sub id='2NC5r'></sub></form><legend id='2NC5r'></legend><bdo id='2NC5r'><pre id='2NC5r'><center id='2NC5r'></center></pre></bdo></b><th id='2NC5r'></th></span></q></dt></tr></i><div id='2NC5r'><tfoot id='2NC5r'></tfoot><dl id='2NC5r'><fieldset id='2NC5r'></fieldset></dl></div>