Skip to content

Instantly share code, notes, and snippets.

@zzramesses

zzramesses/js Secret

Created November 9, 2017 21:57
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 zzramesses/ae378d4c356ecc0bc3cee4bf3a038655 to your computer and use it in GitHub Desktop.
Save zzramesses/ae378d4c356ecc0bc3cee4bf3a038655 to your computer and use it in GitHub Desktop.
ajax code
function initInfiniteScroll(){
var button = $( '.more-holder .more' );
var page = 2;
var loading = false;
button.on( 'click', function(e){
e.preventDefault();
loading = true;
var data = {
action: 'load_more',
query: loadmore.query,
page : page
};
$.post(loadmore.url, data, function(res) {
if( res.success) {
$( '.post-list' ).append( res.data );
initSavedTrips();
page = page + 1;
loading = false;
if ( page > loadmore.max_page ) {
button.parent().remove(); // if last page, remove the button
}
} else {
// console.log(res);
}
}).fail(function(xhr, textStatus, e) {
// console.log(xhr.responseText);
});
});
};
function 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'] ) : 'discover_item';
$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();
if( 'story' === $args['post_type'] ) :
get_template_part( 'partials/story', 'list' );
elseif( 'event' === $args['post_type'] ) :
get_template_part( 'partials/event', 'list' );
else :
get_template_part( 'partials/post', 'list' );
endif;
endwhile; endif; wp_reset_postdata();
$data = ob_get_clean();
wp_send_json_success( $data );
wp_die();
}
global $wp_query;
$args = array(
'url' => esc_url( admin_url( 'admin-ajax.php' ) ),
'query' => $wp_query->query,
'max_page' => $wp_query->max_num_pages,
);
wp_localize_script( 'vp', 'loadmore', $args );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment