I’m having an issue with ajax loading wordpress content with jQuery.
It works just great, except the line returns aren’t being parsed.
It’s being pulled in with json, and stuck into two different divs.
jquery code:
$('.load').on('click',function(e){
e.preventDefault();
var person_clicked = $(this).attr('id');
$.post('/load.php', { id:person_clicked, callback:'person' } )
.done(function(data) {
alert(data);
$('.page_tagline').html(data.title);
$('.left_content').html(data.content);
scrollToElement($('#hero'));
});
return false;
});
load.php:
<?php
/* Send as JSON */
header("Content-Type: application/json", true);
require('../../../wp-load.php');
?>
<?php query_posts('p='.$_POST['id']);
$post = $wp_query->post;
?>
<?php if (have_posts()) : the_post(); ?>
<?php
$returned = array(
'title' => get_the_title(),
'content' => get_the_content(),
);
?>
<?php
echo json_encode($returned);
?>
<?php endif;?>
When I alert it out, the content looks as if the paragraph breaks are in there. There’s a definite spacing between the paragraphs. But when it lands in the div it’s just a giant run-on sentence.
Also, when I load it normally the spacing is there. I tried removing the json part of the ajax, and just loading the whole thing into a div, and the spacing remains so this is for sure a json thing (as far as I can tell). when doing this, the code outputs /r/n/r/n where the new paragraph should be, which is exactly what wordpress does.
Any ideas on how I can make json retain the paragraphs?
You don’t get the automaticly conversion from rowbreaks to p-tags as the function wpautop() isn’t run.
its a filter on the_content. if you want the get_the_content() to have same formating as the_content()
you need to pass it thorugth the filters: