Ready to get started?Download WordPress


Get a post content with proper html tags (3 posts)

  1. danycode
    Posted 2 years ago #

    i'm taking a post in this way inside an ajax request:

    $object_single_post = get_post($post_id);
    $content = $object_single_post->post_content;

    after this task the content of the $content variable is the post with no paragraphs, example:


    \r\nFirst paragraph\r\n\r\nSecond paragraph

    This post content when displayed in a post automatically becomes:


    <p>First paragraph</p>
    <p>Second paragraph</p>

    I'm trying to understand which wordpress function convert the content of the database ( EX.1 ) to the final post ( EX.2 )
    I can convert \r\n to <br /> but i don't like very much this solution and i want the same conversion that wordpress does.

  2. michael.mariart
    Posted 2 years ago #

    What you are looking for is apply_filters(). This lets you apply the differnt filters used in the site, including the content formatting.

    $content = apply_filters ("the_content", $post->post_content);

  3. danycode
    Posted 2 years ago #

    This is exactly what I needed, thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic