WordPress中搜索结果的无限滚动

时间:2019-04-18 13:52:30

标签: javascript php jquery wordpress

在窗口滚动上使用jquery加载结果的下一页,下面的代码已经可以创建无限滚动来加载自定义帖子而不是分页了,我想做的是在自定义搜索中使用相同的逻辑查询

// append load more button to send ajax request on the right moment
$('.project-listings').append('<span class="load-more"></span>');
var button = $('.project-listings .load-more'),
  page = 2,
  loading = false,
  scrollHandling = {
    allow: true,
    reallow: function() {
      scrollHandling.allow = true;
    },
    delay: 400
  };
$(window).scroll(function() {
  if (!loading && scrollHandling.allow) {
    scrollHandling.allow = false;
    setTimeout(scrollHandling.reallow, scrollHandling.delay);
    var offset = $(button).offset().top - $(window).scrollTop();
    if (2000 > offset) {
      loading = true;
      $('.loader-wrap').css({
        display: 'flex'
      });
      var data = {
        'action': 'projects_search_results',
        'page': page,
        'region': AJAX.region,
        'search_text': AJAX.search_text,
      };
      // console.log(data);
      $.post(AJAX.ajaxurl, data, function(response) {
        if (response.data != "") {
          $('.project-listings .content-wrap').append(response.data);
          $('.project-listings').append(button);
          page++;
          $('.loader-wrap').css({
            display: 'none'
          });
          loading = false;
        } else {
          $('.loader-wrap').css({
            display: 'none'
          });
          scrollHandling.allow = false;
        }
      });
    }
  }
});
// Initialize query prams
$ajaxurl = '';
$region = null;
$search_text = null;

// resets query params values
if (!empty($_GET['region'])){
    $region = $_GET['region'];
}
if(!empty($_GET['s'])){
    $search_text = $_GET['s'];
}
if( in_array('sitepress-multilingual-cms/sitepress.php', get_option('active_plugins')) ){
    $ajaxurl .= admin_url( 'admin-ajax.php?lang=' . ICL_LANGUAGE_CODE );
} else{
    $ajaxurl .= admin_url( 'admin-ajax.php');
}

// localize query params variable to js file to submitted using ajax
wp_localize_script( 'custom-search', 'AJAX', array(
    'ajaxurl'  => $ajaxurl,
    'region'  => $region,
    'search_text'  => $search_text,
) );

// Load projects search results
add_action('wp_ajax_nopriv_projects_search_results', 'projects_search_results_callback');
add_action('wp_ajax_projects_search_results', 'projects_search_results_callback');
function projects_search_results_callback() {
    $paged = $_POST['page'];

    $args = array (
        'post_type' => 'project',
        'posts_per_page' => 4,
        'paged' => $paged,
    );
    if ( !empty( $_POST['region'] ) && $_POST['region'] != null ) {
        $args['tax_query'] = array (
            array(
                'taxonomy' => 'regions',
                'terms'    => $_GET['region'],
            ),
        );
    }


    if ( !empty( $_POST['search_text'] ) && $_POST['search_text'] != null ) {
        $args['s'] = $_GET['s'];
    }
    // start buffer 
    ob_start();
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) :
        while($query->have_posts()){ $query->the_post();
            get_template_part("template-parts/projects");
        }
    endif; wp_reset_postdata();
    // start buffered data in data variable
    $data = ob_get_clean();
    wp_send_json_success( $data );
    wp_die();
}

0 个答案:

没有答案