ajax加载wordpress内容会删除换行符/段落标记


ajax loading wordpress content removes linebreaks/paragraph tag

我在使用jQuery加载wordpress内容时遇到ajax问题。它工作得很好,只是没有解析行返回。它被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;
});

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

当我提醒它时,内容看起来好像断段就在那里。段落之间有一定的间距。但当它降落在div中时,它只是一个巨大的连续语句。

此外,当我正常加载时,间距是存在的。我试着删除ajax的json部分,然后将整个东西加载到div中,间距仍然存在,所以这肯定是json的东西(据我所知)。在执行此操作时,代码会在新段落所在的位置输出/r/n/r/n,这正是wordpress所做的。

关于如何让json保留段落,有什么想法吗?

由于函数wpautop()未运行,因此无法自动从换行符转换为p-tag
它是帐篷上的一个过滤器。如果您希望get_the_content()具有与_content相同的格式你需要通过过滤器:

apply_filters('the_content',get_the_content())