last time I fight with Ajax and WordPress. I have a problem using Ajax wants to load the Posts of the same category ... in response is replaced Object, only one entry.
Where to find the problem?
ajax.js
var $fnWritePostGrid = function (idCat) {
var data = {
type: 'POST',
url: ajaxOptions.url,
action: 'kk_load_servicesGrid',
idCat: idCat
};
$.ajax({
type: "POST",
url: ajaxOptions.url,
data: data,
dataType: "json",
success: function (response) {
console.log(response);
}
});
return false;
};
functions.php
$cat_id = $_POST['idCat'];
$args = array(
'category' => $cat_id,
'posts_per_page' => 8,
'order' => 'DESC'
);
$posts = get_posts($args);
foreach($posts as $post) {
$postID = sanitize_text_field($post->ID);
$postTitle = sanitize_text_field($post->post_title);
$postContent = sanitize_text_field($post->post_content);
$response = array(
'ID' => $postID,
'title' => $postTitle,
'content' => $postContent
);
echo json_encode($response);
exit;
}
In summary, the code works but doesn't return an array of entries, only the first entry in the category.
Please help and thanks in advance.