-
-
Save zzramesses/ae378d4c356ecc0bc3cee4bf3a038655 to your computer and use it in GitHub Desktop.
ajax code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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