Support » Fixing WordPress » Cannot use nl2br(). How does WordPress display its content?

  • Resolved Han

    (@hswebdev)


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

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

    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 />
    <ul><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?

Viewing 1 replies (of 1 total)
  • Thread Starter Han

    (@hswebdev)

    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:
    
    <?php
    $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;
    ?>

    Source

Viewing 1 replies (of 1 total)
  • The topic ‘Cannot use nl2br(). How does WordPress display its content?’ is closed to new replies.