-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamr-load-more.php
58 lines (50 loc) · 1.74 KB
/
amr-load-more.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/*
Plugin Name: Load More Posts
Plugin URI: https://www.github.com/amrvignesh
Description: Load more posts on button click.
Version: 1.0
Author: Vignesh A M R
*/
function load_more_posts_scripts() {
wp_enqueue_script( 'load-more-posts', plugin_dir_url( __FILE__ ) . 'load-more-posts.js', array('jquery'), '1.0', true );
wp_localize_script( 'load-more-posts', 'load_more_posts_ajax', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'load_more_posts_scripts' );
function load_more_posts_callback() {
$args = json_decode( stripslashes( $_POST['query'] ), true );
$args['paged'] = $_POST['page'] + 1;
$args['post_status'] = 'publish';
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
get_template_part( 'template-parts/content', 'excerpt' );
endwhile;
endif;
wp_die();
}
add_action('wp_ajax_load_more_posts', 'load_more_posts_callback');
add_action('wp_ajax_nopriv_load_more_posts', 'load_more_posts_callback');
function load_more_posts_button() {
global $wp_query;
$max_pages = $wp_query->max_num_pages;
$args = array(
'total' => $max_pages,
'current' => $current_page,
'prev_next' => true,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
if( $max_pages > 1 ) :
?>
<div id="load-more-posts" class="load-more-posts">
<?php echo paginate_links( $args ); ?>
</div>
<?php
endif;
}
add_action( 'wp_ajax_nopriv_load_more_posts', 'load_more_posts_button' );
add_action( 'wp_ajax_load_more_posts', 'load_more_posts_button' );