Passed
Push — development ( 95a828...f02a5c )
by
unknown
08:19
created

load-more.php ➔ spurs_loadmore_ajax_handler()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
1
<?php
2
/**
3
 * Load More layout.
4
 *
5
 * @package spurs
6
 */
7
8
// Exit if accessed directly.
9
defined( 'ABSPATH' ) || exit;
10
11
if ( ! function_exists( 'spurs_load_more' ) ) {
12
13
	function spurs_load_more() {
14
		global $wp_query; // you can remove this line if everything works for you
15
16
		// don't display the button if there are not enough posts
17
		if (  $wp_query->max_num_pages > 1 )
18
			echo '<div class="spurs_loadmore btn btn-primary btn-lg">More posts</div>'; // you can use <a> as well
19
	}
20
}
21
22
23
function spurs_loadmore_ajax_handler(){
24
 
25
	// prepare our arguments for the query
26
	$args = json_decode( stripslashes( $_POST['query'] ), true );
27
	$args['paged'] = $_POST['page'] + 1; // we need next page to be loaded
28
	$args['post_status'] = 'publish';
29
 
30
	// it is always better to use WP_Query but not here
31
	query_posts( $args );
32
 
33
	if( have_posts() ) :
34
 
35
		// run the loop
36
		while( have_posts() ): the_post();
0 ignored issues
show
Coding Style introduced by
Please always use braces to surround the code block of WHILE statements.
Loading history...
37
 
38
			// look into your theme code how the posts are inserted, but you can use your own HTML of course
39
			// do you remember? - my example is adapted for Twenty Seventeen theme
40
			get_template_part( 'templates/loop/content', get_post_format() );
41
			// for the test purposes comment the line above and uncomment the below one
42
			// the_title();
43
 
44
 
45
		endwhile;
46
 
47
	endif;
48
	die; // here we exit the script and even no wp_reset_query() required!
49
}
50
 
51
 
52
 
53
add_action('wp_ajax_loadmore', 'spurs_loadmore_ajax_handler'); // wp_ajax_{action}
54
add_action('wp_ajax_nopriv_loadmore', 'spurs_loadmore_ajax_handler'); // wp_ajax_nopriv_{action}