Support » Themes and Templates » get_the_content WITH formatting

  • Resolved schajee


    I’ve been trying to highlight the first post and one of the things I needed to do was to get the layout in a specific manner which was to have an image from the post extracted and inserted before the headline. That wasn’t much of a problem with a little preg_match.

    However the next issue is where I’m stumped right now. Since I already have an image representing the post, i need to get the rest of the post WITH formatting, without images and stopped before the <!--more--> tag. The only way I know that I get a formatted text is via the_content(). But since I can’t process the_content() and $post->post_content and get_the_content() are not giving me formatted text, I’m kinda stuck as to how to proceed further.

    I can do a preg_replace or strip_tags to replace images if i can get a formatted string terminated before <!--more-->. But I need that formatted string.

    Thanks in advance…

Viewing 7 replies - 1 through 7 (of 7 total)
  • Nevermind… managed to do some more preg_replace to replace the carriage returns with <p> tags. Works fine now 🙂

    Thanks for the tip.

    Here’s how I did it:

    <?php echo str_replace("\r", "<br />", get_the_content('')); ?>

    There is actually a simpler and more efficient way to control formatting in WordPress posts. Take a look how…

    The default template, inside single.php has this php code to request the post content:

    <?php the_content('<p class="serif">Read the rest of this entry &raquo;
    '); ?>

    If we wanna fully control the output, we call get_the_content instead of just the_content. get_the_content outputs a string, so it’s easier to manipulate it than the_content (which is not a string):

    <?php echo strip_tags(nl2br(get_the_content('')),"<img><b><strong><i><em><a>"); ?>

    In the above example, I’m fetching the post content, create br’s from the carriage returns put by WordPress and then strip all tags except the once specified at the end (<img><b><strong><i><em><a>) . I make sure to add there again, so it won’t be stripped (obviously).

    Hope this helps someone. 😉

    I have solved this problem like the WordPress team did in the core. Read my article at get_the_content() WITH formatting to find out.

    @jenst – thank you! But this renders the use of my ‘Visual/HTML’ tabs, in the back-end posts, broken. Is there a specific place in the functions.php file I should paste this code?


    …[Inside the existing php tags people – doh!]

    Cheers Jenst!


    You don’t have to re-invent the wheel.
    (Or hack the core…)

    Just use output buffering:

    function ob_wp_content($display = true) {
    global $post;
    $output = ob_get_contents();
    if(!$display) {return $output;} else {print $output;};

    Also, wrap the $output so you can choose a simple “$display” flag to make this function either a “getter” or a “printer.” Wrap all the WP functions this way to maintain standards throughout your templates.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘get_the_content WITH formatting’ is closed to new replies.