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

  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; };   

functions.php

  $ 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; }   

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:

  echo json_encode ($ response); Go out;   

Make an array of all the posts you want to return to what you want to do, something like this:

  $ 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;   

In this way you will actually return the object's JSON array instead of a JSON object.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -