C
C
christi1572018-10-26 16:11:00
WordPress
christi157, 2018-10-26 16:11:00

AJAX Lazy load only returns once. Why does the ajax not a function error follow?

I am making a lazy load page. If you do not display posts on the page, then in the console in the network preview, all posts are perfectly loaded one after another. But when I add $('#loadmore').append(data); and I put a div with id in the template, then only one more record is loaded by pressing the button, and then the error $.ajax is not a function follows. How to win it? Thank you!

This is the singl-blog.php code

<main id="main" class="site-main">
    <?php
    query_posts(array(
                'posts_per_page' => 1, 'post_type'=> 'blog',
            ));
            if( have_posts() ){
    while ( have_posts() ) {
      the_post();

      get_template_part( 'templates/template-blog' );
    }
      wp_reset_query();

    }?>
    <div id="loadmore"></div>
<a href="#" id="load-post" title="">Показать еще</a>
<script>
var page = 2,
    post_count = $('#post-count').text(),
    btn_load_post = $('#load-post'),
    url_controller = "http://localhost/streamdream/wp-admin/admin-ajax.php";

$(function(){

    btn_load_post.on('click',function(e){
        e.preventDefault();
        page++;
        $.ajax({
            type       : "GET",
            data       : {posts_per_page : 1, paged: page, action: "test_ajax"},
            dataType   : "html",
            url        : url_controller,
            success    : function(data){
                $('loadmore').append(data);
            }
        });
    });
});
</script>
    </main><!-- #main -->


this is function.php code
add_action( 'wp_ajax_test_ajax',        'test_ajax' ); // For logged in users
add_action( 'wp_ajax_nopriv_test_ajax', 'test_ajax' ); // For anonymous users

function test_ajax(){
  $posts_per_page = (isset($_GET['posts_per_page'])) ? $_GET['posts_per_page'] : 1;
  $paged = (isset($_GET['paged'])) ? $_GET['paged'] : 0;

  query_posts(array(
      'post_type'=> 'blog',
      'posts_per_page' => $posts_per_page,
      'paged'          => $paged
  ));

  if (have_posts()) {
      while (have_posts()){
          the_post();
          get_template_part( 'templates/template-blog' );
      }
  }
  wp_reset_query();
  wp_die();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2018-10-26
@Austin_Powers

Error in selector
$('#loadmore').append(data);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question