Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created September 14, 2018 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/d6191acf4bf19610d39292dd93bf6492 to your computer and use it in GitHub Desktop.
Save billerickson/d6191acf4bf19610d39292dd93bf6492 to your computer and use it in GitHub Desktop.
<?php
/**
* AJAX Load More
* @link http://www.billerickson.net/infinite-scroll-in-wordpress
*/
function be_ajax_load_more() {
$args = isset( $_POST['query'] ) ? array_map( 'esc_attr', $_POST['query'] ) : array();
$args['post_type'] = isset( $args['post_type'] ) ? esc_attr( $args['post_type'] ) : 'post';
$args['paged'] = esc_attr( $_POST['page'] );
$args['post_status'] = 'publish';
ob_start();
$loop = new WP_Query( $args );
if( $loop->have_posts() ): while( $loop->have_posts() ): $loop->the_post();
be_post_summary();
endwhile;
$current = max( 1, $args['paged'] );
$total = $loop->max_num_pages;
if( $total > $current )
echo '<a href="#" class="load-more">Load More</a>';
endif; wp_reset_postdata();
$data = ob_get_clean();
wp_send_json_success( $data );
wp_die();
}
add_action( 'wp_ajax_be_ajax_load_more', 'be_ajax_load_more' );
add_action( 'wp_ajax_nopriv_be_ajax_load_more', 'be_ajax_load_more' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment