the lab of Jason Resnick

WordPress Tip: the_content() vs get_the_content()

I’m pretty sure that we all know exactly what the the_content( ); and get_the_content( ); do in a WordPress template. If not, then refresh your memory over at the Codex.

So what’s the difference? The different is pretty simple actually. If you find yourself annoyed with formatting of the content, more specifically the added p-tags that WordPress puts into the content that you didn’t. Just use get_the_content(); and you will remove those tags.

Comments

5 Responses to WordPress Tip: the_content() vs get_the_content()

  1. Great tip! I hadn’t thought to use get_the_content() to strip the WP-generated Ps.

    The ‘get_’ prefix is really important to be aware of when you write your own functions for WordPress and applies to a bunch of WP functions. The difference is typically return vs. display, ie:

    - bloginfo(‘url’) displays the path outright in your html, while
    - get_bloginfo(‘url’) returns the path, so you can append to/modify it

    • jason says:

      Yeah definitely — the get prefix is just that, getting the value so you can do what you want with it, not just a dump to the screen.

  2. Harvey says:

    Personally I find it massively counter-intuitive that these 2 functions return different values.

    get_the_content should return the same content, but as a variable rather than echoing to screen. I find it pretty whack that it’s a completely different function.

  3. AndyCG says:

    It’s complete madness that these two offer different functionality.

    Another important difference, get_the_content does not auto-embed YouTube videos.

    Seriously infuriating trying to strip tags and cleanup the code output from the_content if get_the_contents is different!

    • jason says:

      I do agree – it is frustrating. For things like videos, I’ve found that in the backend, I try and have the metabox ask for the url, so I know exactly what I’m getting and can display on the front-end.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>