我有一个代码可以向我返回实时帖子更新,但上面有一些错误。
当新帖子发布时,它是在其他帖子下发布的,我想在其他帖子上发布它。
我的代码:
<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>