php - wordpress ajax and foreach - doesn't return an array -
The last time I will fight with Ajax and WordPress. I have a problem in using Ajax, loading posts of the same category ... the object has been changed in response, only one entry
ajax.js functions.php In summary, the code works but does not return an array of entries, only the first entry in the category. It looks like your PHP is the first iteration of your foreach loop The next thread goes out: Make an array of all the posts you want to return to what you want to do, something like this: In this way you will actually return the object's JSON array instead of a JSON object.
var $ fnWritePostGrid = function (idCat) {var data = {type: 'post', url: ajaxOptions.url, verb: 'kk_load_servicesGrid', idCat: idCat}; $ .Azax ({type: "post", url: ajaxuality.Source, data: data, datatype: "jason", success: function (feedback) {console.log (feedback);}}); return false; };
$ cat_id = $ _POST ['idcat']; $ Args = array ('category' = & gt; $ cat_id, 'posts_per_page' => 8, 'order' => 'DESC'); $ Posts = get ($ args); Foreign Currency ($ post $ post) {$ postID = sanitize_text_field ($ post-> ID); $ PostTitle = sanitize_text_field ($ post- & gt; post_title); $ PostContent = sanitize_text_field ($ post- & gt; Post_sign); $ Response = array ('id' = & gt; $ postID, 'title' = & gt; $ postTitle, 'content' = & gt; $ postcontent); Echo json_encode ($ response); Go out; }
echo json_encode ($ response); Go out;
$ responses = array ( ); Foreign Currency ($ post $ post) {$ postID = sanitize_text_field ($ post-> ID); $ PostTitle = sanitize_text_field ($ post- & gt; post_title); $ PostContent = sanitize_text_field ($ post- & gt; Post_sign); $ Response = array ('id' = & gt; $ postID, 'title' = & gt; $ postTitle, 'content' = & gt; $ postcontent); Array_push ($ answer, $ response)} echo json_encode ($ responses); Go out;
Comments
Post a Comment