[resolved] Cannot use nl2br(). How does WordPress display its content? (2 posts)

  1. Han
    Posted 3 years ago #

    I have some content in the database that looks like this:


    WordPress displays the above with proper breaklines and processes the HTML properly when I view that page/post. If I were to use a custom query to pull that content and display it, it would look chunked together, unless I use nl2br() around it. But then I don't want to have the br tags around my list:

    aaaaaaaaaaaaaaaaaaa<br />
    <br />
     <li>bbbbbbbbbbbbbbbbbbb</li><br />
     <li>ccccccccccccccccccc</li><br />
    </ul><br />
    <br />
    dddddddddddddddddddddddddddddddd<br />
    <br />
    eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee<br />

    I'd like to know if WordPress has a function that I could use that takes in a string parameter and displays the content the way it does. Any ideas?

  2. Han
    Posted 3 years ago #

    I resolved this myself after looking harder online:

    When you retrieve the post content from the database you get the unfiltered content. If you want to achieve the same output like WordPress does in its’ posts or pages then you need to apply filter to the content. You can use the following code:
    $post_id = 26;
    $queried_post = get_post($post_id);
    $content = $queried_post->post_content;
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]>', $content);
    echo $content;


Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.