【发布时间】:2013-08-12 23:20:51
【问题描述】:
我在使用 jQuery 加载 wordpress 内容时遇到问题。 它工作得很好,除了行返回没有被解析。 它被 json 拉入,并被困在两个不同的 div 中。
jquery 代码:
$('.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;
});
加载.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;?>
当我提醒它时,内容看起来好像段落中断在那里。段落之间有一定的间距。但是当它落在 div 中时,它只是一个巨大的连续语句。
另外,当我正常加载它时,间距就在那里。我尝试删除 ajax 的 json 部分,然后将整个内容加载到一个 div 中,并且间距仍然存在,所以这肯定是一个 json 的东西(据我所知)。这样做时,代码会输出 /r/n/r/n 新段落应该在哪里,这正是 wordpress 所做的。
关于如何让 json 保留段落的任何想法?
【问题讨论】:
-
您是否担心显示的 HTML 没有换行符或 HTML 源代码(插入时)没有换行符?
-
get_the_content 和 the_content 不同,作为第二次使用过滤器,试试
apply_filters('the_content',get_the_content()) -
@Puggan,您能否将您的评论作为答案,以便我接受。有效!谢谢。
-
当然,只需要先用事实来支持它:-)