-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic load more
166 lines (133 loc) · 4.23 KB
/
basic load more
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
Loader css:
@keyframes load {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
&.loading {
pointer-events: none;
border-color: black;
&::before {
content: "";
border: 2px solid #f3f3f3;
border-radius: 50%;
border-top: 2px solid #7BB04D;
width: 20px;
height: 20px;
-webkit-animation: load 1s infinite linear;
animation: load 1s infinite linear;
}
}
JS:
window.addEventListener('DOMContentLoaded', () => {
let loadMore_btn = document.querySelector('.load-more');
if (loadMore_btn) {
state.max_pages = loadMore_btn.dataset.maxPages
loadMore_btn.addEventListener('click', async () => {
loadIndicator(true)
await loadMore();
document.querySelector('.grid').insertAdjacentHTML('beforeend', state.lastResult.html);
if (state.finished) {
loadMore_btn.remove()
} else {
loadIndicator(false)
}
});
}
});
let state = {
paged: 1,
ppp: 6,
finished: false,
lastResult: null
}
async function loadMore() {
try {
const formData = new FormData();
formData.append('action', "fetch_expositions");
formData.append('ppp', state.ppp);
formData.append('paged', state.paged + 1);
const response = await fetch('/wp-admin/admin-ajax.php', {
body: formData,
method: "post"
});
const responseBody = await response.text();
state.lastResult = await JSON.parse(responseBody);
if (responseBody) {
state.paged++;
if (state.paged >= state.max_pages) {
state.finished = true;
}
}
} catch (e) {
console.log(e.message);
}
}
function loadIndicator(active) {
let loadMore_btn = document.querySelector('.load-more');
loadMore_btn.disabled = active ? true : false;
loadMore_btn.innerHTML = active ? '' : 'Laad meer';
if (active) {
loadMore_btn.classList.add('loading');
} else {
loadMore_btn.classList.remove('loading');
}
}
PHP
add_action('wp_ajax_fetch_expositions', [$this, 'fetch_expositions_callback']);
add_action('wp_ajax_nopriv_fetch_expositions', [$this, 'fetch_expositions_callback']);
// fetch_expositions
function fetch_expositions_callback()
{
$paged = $_POST['paged'];
$ppp = $_POST['ppp'];
$query_args = [
'post_type' => 'exposition',
'post_status' => 'publish',
// 'max_num_pages' => 1,
'posts_per_page' => $ppp,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC',
];
$query = new WP_Query($query_args);
if ($query->have_posts()) :
ob_start();
while ($query->have_posts()) :
$query->the_post();
$thumbnail = get_the_post_thumbnail(null, 'byron-thumb', [
'class' => 'product-img w-100 h-100'
]);
if(empty($thumbnail)):
$img = get_field('fallback_img', 'option');
$thumbnail = wp_get_attachment_image($img['ID'], 'byron-thumb', false, [
'class' => 'product-img w-100 h-100'
]);
endif;
$link = get_the_permalink();
$title = get_the_title();
$description = mb_strimwidth(get_field('content'), 0, 160) . ".. <span class='text-secondary'>Leer meer</span>";
$date = get_field('date');
get_template_part('template-parts/cards/card-exposition', null, [
'thumbnail' => $thumbnail,
'link' => $link,
'title' => $title,
'description' => $description,
'date' => $date
]);
endwhile;
$output = ob_get_contents();
ob_end_clean();
$result = [
'html' => $output
];
echo json_encode($result);
exit;
wp_reset_query();
else :
echo '<div class="g-col-12 text-center no-results">No posts found.</div>';
endif;
// Reset post data and stop execution of the functions.
wp_reset_postdata();
wp_reset_query();
wp_die();
}