WP Snipp Ninja

What the_content and get_the_content are used for in WordPress?

Author: Dimitar Radev | Posted on: June 11, 2020 |


The WordPress functions the_content and get_the_content are used to pull the main text area from the page or post.

The difference between them is in the way they are used. the_content() echoes WordPress editor’s content and it’s used inside the template files while get_the_content() is fetching it to be used in the PHP code as a variable.

Please note that there is also the_content filter which is used to apply styling and shortcodes by running a chain of filters like wp_autop which adds the tags. The full list of running filters trough the_content filter are wptexturize, convert_smilies, convert_chars, shortcode_unautop and maybe one of the most used ones do_shortcode.

Now we can dig deeper into the functions.

get_the_content pulls the copy directly from the database as a raw string containing the unwrapped shortcodes. In order to make that useful, you will need to pass the data through the_content filter which will give you an appropriate HTML output.

$content = apply_filters( 'the_content', get_the_content() );

And last but definitely not last is the_content. This function will echo the properly formatted content directly into your template. In its essence the_content calls get_the_content and applies the_content filter before echoing the result.

Except for a few minor things I skipped, now you should have a better picture in which content function or filter does what. An interesting thing to mention is that currently (WP ver 5.2) there is a warning that pops up with PHP 7.2 environments. The warning states “Warning: count(): Parameter must be an array or an object that implements Countable in /wp-includes/post-template.php on line 284”. For more information on that please check our article How to Fix PHP 7.2 “Warning: count()” on get_the_content() or the_content() in WordPress.

 

TL;DR

WordPress Codex / Filter Reference / the content

WordPress Codex / Functions / the_content()

Parameters:
$more_link_text – Accepts string, optional. Changes the default string used to generate the read more text for the link when the more tag is used in the content. Defaults to null.

$strip_teaser – Accepts boolean (true or false), optional. When we set this parameter to true the content before the read more link should be removed. Literally, meaning: Remove the teaser content. Here I need to note that in my current environment I wasn’t able to get it working. It appears that there may be an issue with WP 5. Defaults to false.

WordPress Codex / Functions / get_the_content()

Parameters:
$more_link_text – String, Defaults to null.
$strip_teaser – Boolean, Defaults to false
$post – It is used to pull the content for a particular post or page. Accepts the WP_Post object or an integer with the post ID. Defaults to null.

 

I hope this article was helpful.
Have a great day!

FAQs:

What is the difference between the_content and get_the_content functions in WordPress?

The short answer would be that the_content() function echoes the main page or post content writing area (a.k.a as WP Editor or WYSIWYG) and get_the_content is usually used as a variable inside a function. However, something to remember is that in order to properly display a result from get_the_content() function you will need to pass it trough the_content filter.

How to apply the “the_content” filter in WordPress?

Simply assign the apply_filters function to a variable as shown on the snippet below:

$php_var_name = apply_filters( 'the_content', get_the_content() );

Article keywords

  • About the Author

     Dimitar Radev

    Hey, I'm Dimitar "B1-0S" Radev or D in short. I'm a WordPress Developer for nearly 10 years and I have in-depth knowledge in creating Custom WordPress Themes, Plugins, Commercial Themes Edits, and WooCommerce Storefronts. In the last two years, I'm specializing in Technical SEO for WordPress websites.


    Follow Dimitar Radev on Social Media

    Leave a Reply

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

  • How we can add x number of custom elements like Advertising Banner, Google Adsense code, or just 2 columns to add some data in our content loop after x number of paragraphs or after x number of heading tags like h2,h3,h4? More clear may can be this question – How to add element after specific numbers of content elements in our the_content or get_the_content page or post loops?