Order result from ajax

时间:2017-11-14 作者:Mehran Pourjenabi

我有一个代码可以向我返回实时帖子更新,但上面有一些错误。

当新帖子发布时,它是在其他帖子下发布的,我想在其他帖子上发布它。

我的代码:

<div class="latest-news">
      <?php 
        $current_posts_ids = array();
        $args = array(
          \'post_type\' => \'post\',
          \'post_status\' => \'publish\',
        );
        $my_posts = new WP_Query( $args );

        if ( $my_posts->have_posts() ) : ?>
          <div class="posts">
            <?php while ( $my_posts->have_posts() ) : $my_posts->the_post() ?>
              <?php array_push($current_posts_ids, get_the_ID() );?>
              <article data-id="<?php echo get_the_ID()?>">
                <h2><?php the_title() ?></h2>
                <p><?php the_excerpt() ?><p>
              </article>
            <?php endwhile ?>
          </div>
        <?php endif ?>
 </div>

 <script>
  $(document).ready(function(){

      var ajaxurl = "<?php echo admin_url( \'admin-ajax.php\' ); ?>";   
      var dataArray =  <?php echo json_encode($current_posts_ids) ?>;

      function getMyPosts() {

            data = { action: "update_news", data: dataArray};
            $.post(ajaxurl, data, function(response) {
                // reset array
                dataArray = [];
                // append the new news posts                
                $(\'.latest-news .posts\').append(response);
                // create new array of ids to exclude
                $(\'article\').each(function(){
                    dataArray.push($(this).data(\'id\'));  
                }); 
                // repeat function after 5 seconds
                setTimeout(function () {
                    getMyPosts();
                }, 5000)
            });

     }
     //run the function first time
     getMyPosts();   

});
</script>

1 个回复
SO网友:Drupalizeme

所以您可以使用prepend 方法:

 <script>
  $(document).ready(function(){

      var ajaxurl = "<?php echo admin_url( \'admin-ajax.php\' ); ?>";   
      var dataArray =  <?php echo json_encode($current_posts_ids) ?>;

      function getMyPosts() {

            data = { action: "update_news", data: dataArray};
            $.post(ajaxurl, data, function(response) {
                // reset array
                dataArray = [];
                // append the new news posts                
                $(\'.latest-news .posts\').prepend(response);
                // create new array of ids to exclude
                $(\'article\').each(function(){
                    dataArray.push($(this).data(\'id\'));  
                }); 
                // repeat function after 5 seconds
                setTimeout(function () {
                    getMyPosts();
                }, 5000)
            });

     }
     //run the function first time
     getMyPosts();   

});
</script>

结束

相关推荐

Admin-由于$_请求为空,AJAX响应为0

我强烈反对用延迟加载内容来更新现有的客户端站点。目前,问题似乎在管理ajax中。php文件,其中脚本在if ( empty( $_REQUEST[\'action\'] ) ) die( \'0\' ); 控制台中的错误:GEThttp://www.example.com/wp-admin/admin-ajax.php?lang=en&action=example_frontpage_feed&page=0&next_time=0 net::ERR\\u EM