get_the_content() WITH formatting
August 31st, 2008Normally the get_the_content() tag returns the content without formatting. I found out a solution to make get_the_content() tag return the same content as the_content().
<?php echo get_the_content(); ?> <?php the_content(); ?>
For me and others that can be a problem if you expect them to behave the same. I looked into the Wordpress core and found a solution. Put this code into your functions.php in your theme folder.
function get_the_content_with_formatting ($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
$content = get_the_content($more_link_text, $stripteaser, $more_file);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
In your theme, call the function within the loop:
<?php get_the_content_with_formatting(); ?>
To get more new functions/template tags, install my plugin WP Extra Template Tags.
Thanks, very helpful, saved me a lot of research and work.
/Tim
Do you know why the_content() replaces ]]> by ]]>?
I’m searching for the reason and Google linked your post. This str_replace() is breaking my JavaScript
Thank you sir, very useful
This has been very useful – thank you. It does make you wonder, however, why this sort of stuff isn’t standard in wordpress. One day, maybe.
Thank you very much
Oh, yeahhh… works great, thxs!
Thanks, Jenst, this is fantastic. And it works on older versions of WP too!
Michael, regarding your question about “Is there a way to get just a snippet of the get_the_content part? Like say the first 30 characters?”… I strip the tags off a call of the content — in this case using get_the_content_with_formatting — then do an explode using spaces, then pull it back together with the first “X” words of the resultant array. Hope that helps!
Works like a charm
Thanks
You are a star!
Thanks solved my problem!
Cheers
Chris.
My hero!
L
wow great work!
you just saved me a lot of work. thank you so much!
Nice one! Cheers.
Thanks! Another idea is to use the output buffering to grab the string directly from the_content, in order to be safe from future updates.
Thanks for that. Exactly what I was looking for!
This was tremendously helpful to me, thanks!
Hi! Cool post!
Is there a way to get just a snippet of the get_the_content part? Like say the first 30 characters?
Unfortunately the_content_rss doesn’t work on my version, and I’ve resorted to using get_the_content.
Thanks for any advice.