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 mx-auto w-25">More posts</div>'; // you can use <a> as well |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
function jd_my_load_more_scripts() { |
23
|
|
|
|
24
|
|
|
global $wp_query; |
25
|
|
|
|
26
|
|
|
// In most cases it is already included on the page and this line can be removed |
27
|
|
|
wp_enqueue_script('jquery'); |
28
|
|
|
|
29
|
|
|
// register our main script but do not enqueue it yet |
30
|
|
|
//wp_register_script( 'my_loadmore', get_stylesheet_directory_uri() . '/js/myloadmore.js', array('jquery') ); |
31
|
|
|
|
32
|
|
|
// now the most interesting part |
33
|
|
|
// we have to pass parameters to myloadmore.js script but we can get the parameters values only in PHP |
34
|
|
|
// you can define variables directly in your HTML but I decided that the most proper way is wp_localize_script() |
35
|
|
|
wp_localize_script( 'spurs-scripts', 'spurs_loadmore_params', array( |
36
|
|
|
'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX |
37
|
|
|
'posts' => json_encode( $wp_query->query_vars ), // everything about your loop is here |
38
|
|
|
'current_page' => get_query_var( 'paged' ) ? get_query_var('paged') : 1, |
39
|
|
|
'max_page' => $wp_query->max_num_pages |
40
|
|
|
) ); |
41
|
|
|
|
42
|
|
|
wp_enqueue_script( 'my_loadmore' ); |
43
|
|
|
} |
44
|
|
|
add_action( 'wp_enqueue_scripts', 'jd_my_load_more_scripts' ); |
45
|
|
|
|
46
|
|
|
function spurs_loadmore_ajax_handler(){ |
47
|
|
|
|
48
|
|
|
// prepare our arguments for the query |
49
|
|
|
$args = json_decode( stripslashes( $_POST['query'] ), true ); |
50
|
|
|
$args['paged'] = $_POST['page'] + 1; // we need next page to be loaded |
51
|
|
|
$args['post_status'] = 'publish'; |
52
|
|
|
$search_page = $_POST['search_page']; |
53
|
|
|
|
54
|
|
|
// it is always better to use WP_Query but not here |
55
|
|
|
query_posts( $args ); |
56
|
|
|
|
57
|
|
View Code Duplication |
if( have_posts() ) : |
|
|
|
|
58
|
|
|
|
59
|
|
|
// run the loop |
60
|
|
|
while( have_posts() ): the_post(); |
|
|
|
|
61
|
|
|
|
62
|
|
|
// look into your theme code how the posts are inserted, but you can use your own HTML of course |
63
|
|
|
// do you remember? - my example is adapted for Twenty Seventeen theme |
64
|
|
|
if( true == $search_page ){ |
65
|
|
|
get_template_part( 'templates/loop/content', 'search' ); |
66
|
|
|
} else { |
67
|
|
|
get_template_part( 'templates/loop/content', get_post_format() ); |
68
|
|
|
} |
69
|
|
|
// for the test purposes comment the line above and uncomment the below one |
70
|
|
|
// the_title(); |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
endwhile; |
74
|
|
|
|
75
|
|
|
endif; |
76
|
|
|
die; // here we exit the script and even no wp_reset_query() required! |
77
|
|
|
} |
78
|
|
|
add_action('wp_ajax_loadmore', 'spurs_loadmore_ajax_handler'); // wp_ajax_{action} |
79
|
|
|
add_action('wp_ajax_nopriv_loadmore', 'spurs_loadmore_ajax_handler'); // wp_ajax_nopriv_{action} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.