WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. danycode
    Member
    Posted 1 year ago #

    Hallo,
    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:

    EX.1

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

    This post content when displayed in a post automatically becomes:

    EX.2

    <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
    Member
    Posted 1 year 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
    Member
    Posted 1 year ago #

    This is exactly what I needed, thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic