-
-
Save tony-keller/b19feada5055e948570cb135a570ffc7 to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* Javascript for Load More | |
* | |
*/ | |
function be_load_more_js() { | |
global $wp_query; | |
$args = array( | |
'nonce' => wp_create_nonce( 'be-load-more-nonce' ), | |
'url' => admin_url( 'admin-ajax.php' ), | |
'query' => $wp_query->query, | |
); | |
wp_enqueue_script( 'be-load-more', get_stylesheet_directory_uri() . '/js/load-more.js', array( 'jquery' ), '1.0', true ); | |
wp_localize_script( 'be-load-more', 'beloadmore', $args ); | |
} | |
add_action( 'wp_enqueue_scripts', 'be_load_more_js', 10 ); | |
/** | |
* AJAX Load More | |
* | |
*/ | |
/* | |
* Use your query here. Remember that if you make a call to $custom->the_post() | |
* you'll need to reset the post data after the loop by calling wp_reset_postdata(). | |
*/ | |
function be_ajax_load_more() { | |
check_ajax_referer( 'be-load-more-nonce', 'nonce' ); | |
$args = isset( $_POST['query'] ) ? $_POST['query'] : array(); | |
$args['post_type'] = isset( $args['post_type'] ) ? $args['post_type'] : 'post'; | |
$args['paged'] = $_POST['page']; | |
$args['post_status'] = 'publish'; | |
$args['posts_per_page'] = 2; | |
$args['offset'] = 7 + 2 * ( $_POST['page'] - 2 ); | |
$loop = new WP_Query( $args ); | |
if( $loop->have_posts() ): while( $loop->have_posts() ): $loop->the_post(); | |
echo '<h1>' . the_title() . '</h1>'; | |
echo '<p>' . $args['paged'] . '</p>'; | |
endwhile; endif; wp_reset_postdata(); | |
$data = ob_get_clean(); | |
wp_send_json_success( $data ); | |
} | |
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