dq13733519262 2013-12-09 08:18 采纳率: 0%
浏览 34

使用Ajax和Jquery一次以块100加载Wordpress帖子

I would like some help to change a bit og Jquery/Ajax code. The following code loads a new Wordpress post every time i scroll to the bottom

jQuery(function($){
    var page = 1;
    var loading = true;
    var $window = $(window);
    var $content = $("body #ordrene");
    var load_posts = function(){
            $.ajax({
                type       : "GET",
                data       : {numPosts : 1, pageNumber: page},
                dataType   : "html",
                url        : "http://www.mysite.com/wp-content/themes/twentythirteen-child/loopHandler.php",
                beforeSend : function(){
                    if(page != 1){
                        $content.append('<div id="temp_load" style="text-align:center">' +
                            '<img src="../images/ajax-loader.gif" />' +
                            '</div>');
                    }
                },
                success    : function(data){
                    $data = $(data);
                    if($data.length){
                        $data.hide();
                        $content.append($data);
                        $data.fadeIn(500, function(){
                            $("#temp_load").remove();
                            loading = false;
                        });
                    } else {
                        $("#temp_load").remove();
                    }
                },
                error     : function(jqXHR, textStatus, errorThrown) {
                    $("#temp_load").remove();
                    alert(jqXHR + " :: " + textStatus + " :: " + errorThrown);
                }
        });
    }
    $window.scroll(function() {
        var content_offset = $content.offset();
        console.log(content_offset.top);
        if(!loading && ($window.scrollTop() +
            $window.height()) > ($content.scrollTop() + $content.height() + content_offset.top)) {
                loading = true;
                page++;
                load_posts();
        }
    });
    load_posts();
}); 

On my site i have about 1000 posts, all which i want to display on one page so that i am able to sort them with Jquery Sortable. But the server i am on can't handle that many posts loading i one go, so i would like to ue ajax to break the query to the database up into multiple chunks.

Therefor i would like to alter the code above (or get advice on brand new code) to do the following: On page load, get 100 posts. When the first 100 posts are loaded, fetch 100 more, And so on until all my Wordpress posts are loaded.

If anyone is able to help me, i would really appreciate it!

Anders

UPDATE:

My loopHandler.php file looks like this

<?php
// Our include
define('WP_USE_THEMES', false);
require_once('../../../wp-load.php');

// Our variables
$numPosts = (isset($_GET['numPosts'])) ? $_GET['numPosts'] : 0;
$page = (isset($_GET['pageNumber'])) ? $_GET['pageNumber'] : 0;

echo $numPosts;
echo $page;

$kundenavn = get_the_title();
$category_id = get_cat_ID($kundenavn);

query_posts('cat=' . $category_id . '&posts_per_page=' . $numPosts . '&paged=' . $page);    

if (have_posts()) : while (have_posts()) : the_post(); ?>

    //Content of the loop 

<?php       
endwhile;
endif;

wp_reset_query();
?>
  • 写回答

1条回答 默认 最新

  • duanhe1965 2013-12-09 09:29
    关注

    You could try something like this:

    jQuery(function($) {
      var page = 1;
      var loading = true;
      var $window = $(window);
      var $content = $("body #ordrene");
      var load_posts = function() {
        console.log("Loading posts, page is:",page);
        $.ajax({
          type: "GET",
          data: {numPosts: 100, pageNumber: page}, //changed this
          dataType: "html",
          url: "http://www.kundelogg.no/wp-content/themes/twentythirteen-child/loopHandler.php",
          beforeSend: function() {
            if (page !== 1) {
              $content.append('<div id="temp_load" style="text-align:center">' +
                      '<img src="../images/ajax-loader.gif" />' +
                      '</div>');
            }
          },
          success: function(data) {
            console.log("Received data, length is:",data.length);
            $data = $(data);
            if ($data.length) {
              $data.hide();
              $content.append($data);
              $data.fadeIn(500, function() {
                $("#temp_load").remove();
                loading = false;
              });
            } else {
              $("#temp_load").remove();
            }
            if (data === "")//make sure php returns empty string if no more posts
              return;
            pageNumber++;//added this
            load_posts();//added this
          },
          error: function(jqXHR, textStatus, errorThrown) {
            $("#temp_load").remove();
            alert(jqXHR + " :: " + textStatus + " :: " + errorThrown);
          }
        });
      };
      load_posts();
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图