/
simple-ajax-load-posts.php
53 lines (45 loc) · 1.18 KB
/
simple-ajax-load-posts.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
<?php
/**
* Plugin Name: Simple AJAX Load Posts
* Plugin URI: http://www.github.com/eiffelqiu
* Description: Load the next page of posts with AJAX.
* Version: 0.1
* Author: eiffel qiu
* Author URI: http://www.github.com/eiffelqiu
*/
/**
* Initialization. Add our script if needed on this page.
*/
function salp_init()
{
global $wp_query;
if (!is_singular()) {
wp_enqueue_style(
'salp-style',
plugin_dir_url(__FILE__) . 'css/style.css',
false,
'1.0',
'all'
);
wp_enqueue_script(
'salp-script',
plugin_dir_url(__FILE__) . 'js/load-posts.js',
array('jquery'),
'1.0',
false
);
$max = $wp_query->max_num_pages;
$paged = (get_query_var('paged') > 1) ? get_query_var('paged') : 1;
// Add some parameters for the JS.
wp_localize_script(
'salp-script',
'salp',
array(
'startPage' => $paged,
'maxPages' => $max,
'nextLink' => next_posts($max, false)
)
);
}
}
add_action('template_redirect', 'salp_init');