Я пытаюсь получить сообщение, используя get_posts
с slug, который успешно получен от ajax. Однако это ничего не возвращает. Пожалуйста, укажите на мою ошибку.
Мой Аякс:
var nextt = document.getElementById("nextt");
nextt.addEventListener("click",function(e){
event.preventDefault();
var nextpage = document.querySelector("a[rel=next]").getAttribute("href");
xhr = new XMLHttpRequest();
xhr.open('POST', ajaxurl, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
xhr.onreadystatechange = function(){
var content = document.getElementById('main');
if(xhr.readyState == 4){
if(xhr.status == 200) {
content.innerHTML = xhr.responseText;
} else{
content.innerHTML = 'Cannot connect to server. Check your internet connection '}
}
};
xhr.send('action=load_selanjutnya&nonce=' + ajaxnonce + '&value=' + nextpage);
},false);
На моей функции. Php
function nextpage(){
global $post;
print_r($_REQUEST);
$slug = $_REQUEST['value'];
$args=array(
'name' => $slug,
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => 1
);
$my_posts = get_posts( $args );
if( $my_posts ) {
foreach ($my_posts as $post)
setup_postdata( $post );
get_template_part( 'single-ajax', get_post_format() );
wp_reset_postdata();
}
else {echo $slug . ' - Fail. '; var_dump($my_posts);}
exit();
}
add_action( 'wp_ajax_nextpage', 'nextpage' );
add_action( 'wp_ajax_nopriv_nextpage', 'nextpage' );
Я ожидаю, что он загрузит страницу содержимого вместо ничего / пусто в качестве возврата.