In my functions.php file I have a function:
function get_news_recipes()
{
global $wpdb;
$interval=$_REQUEST['interval'];
$getPosts = $wpdb->get_results("SELECT ID FROM wp_posts WHERE post_status='publish' AND post_type='post' LIMIT 3 OFFSET ".$interval."");
foreach ($getPosts as $blog)
{
$id=$blog->ID;
$title=get_the_title($id);
$url=get_permalink($id);
$content=get_the_content($id);
$image=wp_get_attachment_url(get_post_thumbnail_id($id));
$postArray[$id]=array(
'title'=>$title,
'content'=>$content,
'url'=>$url,
'image'=>$image
);}
echo wp_send_json($postArray);
die();
}
and in my scripts files I have the ajax setup like this:
function getNewsPosts(interval) {
jQuery.ajax({
url : ajaxurl,
type : 'post',
data : {
action : 'get_news_recipes',
interval:interval,
},
success : function( response ){
//get and store references to modules in slides
var imgs = jQuery('.js-slideImg');
var titles = jQuery('.js-slideTitle');
var contents = jQuery('.js-slideContent');
var links = jQuery('.js-slideURL');
var count = 0;
//interate over each slide and replace html
for(var key in response){
if(response.hasOwnProperty(key)){
jQuery(imgs[count]).css('background-image', 'url('+response[key].image+')');
jQuery(titles[count]).html(response[key].title);
jQuery(contents[count]).html(response[key].content);
jQuery(links[count]).attr('href', response[key].url);
count++;
}
}
}
});
}
Everything works great except the content in response is coming back empty and I can't figure out why. The image url, actual url, and title all come back as expect, however the content is an empty string.
The function get_the_content must be used in the loop otherwise it won't return anything. You should use the following code to the the content of a post.
$post = get_post($id);
$content = $post->post_content;
Modifying your code it would be the following
$id=$blog->ID;
$content=$blog->post_content;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.