Show post published live

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

我想在不刷新的情况下显示我发布的新帖子的实时页面。有点像博客网站上的突发新闻。

我该怎么做?有人知道吗?

2 个回复
最合适的回答,由SO网友:Jim-miraidev 整理而成

您可以为ajax请求创建一个函数,并设置超时以使其重复。您还需要发送当前正在显示的新闻文章的ID,以便从PHP中的新WP\\U查询中排除它们

更新-现在使用数据属性创建post ID数组,以排除下一个ajax请求。

居住php

<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>
功能。php

add_action( \'wp_ajax_update_news\', \'update_news\' );
add_action(\'wp_ajax_nopriv_update_news\', \'update_news\');
function update_news() {
    global $wpdb;
    $data = $_POST[\'data\'];

    $args = array(
        \'post_type\' => \'post\',
        \'post_status\' => \'publish\',
        \'post__not_in\' => $data,
    );
    $my_posts = new WP_Query( $args );

    if($my_posts->have_posts()) :
    while($my_posts->have_posts()) : $my_posts->the_post();          
        echo "<article data-id=\'".get_the_ID()."\'>";
        echo "<h2>" . get_the_title() . "</h2>";
        echo "<p>" . get_the_excerpt() . "</p>";
        echo "</article>";
    endwhile;
    endif;

 wp_die();

}

SO网友:Mehran Pourjenabi

我的生活。php页面:

<!DOCTYPE html>
<html lang="fa">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <?php wp_head(); ?>

  </head>
  <body>

    <script type="text/javascript">
    var ajaxurl = "<?php echo admin_url( \'admin-ajax.php\' ); ?>";

    function getMyPosts() {
            // get the current ids of the post on that page using a
            var $current_posts = [1,2,3] //example of id array

            var data = {
                \'action\': \'update_news\',
                \'posts\': 1,
                \'security\': \'<?php echo wp_create_nonce("load_more_posts"); ?>\'
            };

            $.post(ajaxurl, data, function(response) {
                // append the new news posts
                $(\'.latest-news\').append(response);
                // repeat function after 5 seconds
                setTimeout(function () {
                    getMyPosts();
                }, 5000)
            });
     }
     //run the function first time
     getMyPosts();
    </script>

  </body>
</html>
和我的功能代码:

function live_post() {

  global $wpdb; // this is how you get access to the database

  $data = $_POST[\'data\'];
  $current_posts = $_POST[\'posts\'];
  $args = array(
      \'post_type\' => \'post\', // add post type
      \'post_status\' => \'publish\',
      \'posts_per_page\' => \'-1\',
      \'post__not_in\' => array( $current_posts )
  );
  //Query the posts
  $my_posts = new WP_Query( $args );
  if ( $my_posts->have_posts() ) :
      ?>
      <?php while ( $my_posts->have_posts() ) : $my_posts->the_post() ?>
          <h2><?php the_title() ?></h2>
          <?php the_excerpt() ?>
      <?php endwhile ?>
      <?php
  endif;
  wp_die();
  }


add_action( \'wp_ajax_update_news\', \'live_post\' );

结束

相关推荐

将PRE_GET_POSTS用于ACF中继子字段上相似比较的元值

我正在尝试根据repeater字段的ACF子字段值组织搜索结果页面。我想要一个相似的结果,而不是=。到目前为止,我获得了以下信息(在下面的帮助下进行了修改,但仍然不起作用):// Modify meta key to allow wildcard function add_wildcard_to_meta_key_filter( $where ) { $where = str_replace(\"meta_key = \'test_repeater_%\", \"meta_k