【问题标题】:ajax loading wordpress content removes linebreaks/paragraph tagajax 加载 wordpress 内容删除换行符/段落标签
【发布时间】: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,您能否将您的评论作为答案,以便我接受。有效!谢谢。
  • 当然,只需要先用事实来支持它:-)

标签: php jquery json wordpress


【解决方案1】:

由于函数 wpautop() 未运行,您不会自动将行分隔符转换为 p-tags。
它是the_content 上的过滤器。如果您希望 get_the_content() 具有与 the_content() 相同的格式 您需要通过过滤器:

apply_filters('the_content',get_the_content())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-09
    • 2011-07-10
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    相关资源
    最近更新 更多