页面滚动

时间:2017-11-26 20:14:45

标签: php jquery ajax wordpress post

我希望在单个帖子页面中滚动时使用ajax加载或添加相关帖子。但是我获得了404的Ajax状态。有人可以帮助我吗?

函数文件:

add_action('wp_ajax_load_posts_by_zi', 'related_posts');
add_action('wp_ajax_nopriv_load_posts_by_zi', 'related_posts');

function related_posts() {

  $post_tag = wp_get_object_terms( get_the_ID(), 'post_tag', array( 'fields' => 'ids' ) );

  //$paged = $_POST['page'];

  $args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 5,
    /*'paged' => $paged,*/
    'caller_get_posts'=>1,
    'tax_query'=> array(
      array(
       'taxonomy' => 'post_tag',
       'field'    => 'term_id',
       'terms'    => $post_tag
      )
    ),
    'post__not_in' => array($post->ID)
  );

  $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; wp_reset_postdata(); ?>
    <?php
  endif;

  die();
}

single.php

    <main id="main" class="site-main " role="main">
       <?php while ( have_posts() ) : the_post(); ?>
       <?php get_template_part( 'loop-templates/content', 'single' ); ?>

       <?php endwhile; // end of the loop. ?>

    </main><!-- #main -->
    <div class="above-comment">
    <?php get_template_part('widget-templates/above-comment', 'widget') ;?>
     </div>
    <?php
   // If comments are open or we have at least one comment, load up the comment template

   if ( comments_open() || get_comments_number() ) :
       comments_template();
   endif;

   ?>
   <div class="loadMorePosts">loadmore</div>

js / jquery文件

$(window).scroll(function() {
        if ($(window).scrollTop() == $(document).height() - 

    $(window).height()) {

                $.ajax({

                    url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
                    type: 'GET',
                    dataType: 'html',
                    cache: false,
                    data: {
                        action: 'load_posts_by_zi'
                    },
                    complete: function(loadMorePosts) {
                        $container.append(loadMorePosts);
                    }
                });
            }
    });

我希望能够更新网址,但我不确定如何添加该网址。有人可以帮助我吗?

0 个答案:

没有答案