[resolved] get_the_content WITH formatting (8 posts)

  1. schajee
    Posted 8 years ago #

    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...

  2. schajee
    Posted 8 years ago #

    Nevermind... managed to do some more preg_replace to replace the carriage returns with <p> tags. Works fine now :)

  3. datafeedr
    Posted 8 years ago #

    Thanks for the tip.

    Here's how I did it:

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

  4. joomlaworks
    Posted 7 years ago #

    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. ;)

  5. Jenst
    Posted 7 years ago #

    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.

  6. ldexterldesign
    Posted 7 years ago #

    @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?


  7. ldexterldesign
    Posted 7 years ago #

    ...[Inside the existing php tags people - doh!]

    Cheers Jenst!


  8. jasongrier
    Posted 7 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic