Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress: difference between get_the_content() and the_content()

Tags:

php

wordpress

I was working on the new theme in the WordPress and spent tons of time with the get_the_content() function.

  <div class="clearfix">
      <div>
        <p><?=get_the_content();?></p>
      </div>
  </div>

Seems that it doesn't process shortcuts and doesn't do paragraphs.

Then I replaced it with the the_content(); and my paragraphs and shortcuts started to work.

  <div class="clearfix">
      <div>
        <p><?=the_content();?></p>
      </div>
  </div>

My question: What is the difference between the functions and what additional processing the_content(); does comparing to get_the_content();?

like image 497
Pavel Molchanov Avatar asked Jun 17 '18 00:06

Pavel Molchanov


People also ask

What is the_ content?

By design, the_content() tag includes a parameter for formatting the <! --more--> content and look, which creates a link to “continue reading” the full post. Notes about <!

How do I find page contents page ID in WordPress?

If you need to get the page ID you can easily do this by logging into your WordPress website admin and opening the page, post or custom post type for editing. The page id is visible in the address bar as shown in the screenshot below.

How do I show limited post content in WordPress?

Limiting the words or characters during the display can be done by either creating a function in the function. php file or right in the code itself using the WordPress function wp_trim_words() or mb_strimwidth() etc. There are two ways to display content using the_content() and get_the_content() function.


2 Answers

While @J Quest provided an adequate answer, I'd like to elaborate a little bit. Generally speaking, WordPress has two types of post variable functions: get_ functions and the_ functions.

get_ functions, such as get_the_content() or get_the_ID()will return the desired information, which must then be manipulated and printed to the page. Some examples:

$content = get_the_content();
$content = apply_filters( 'the_content', $content );
$content = str_replace( 'foo', 'bar', $content );

echo 'Post #'. get_the_ID() . $content;

the_ functions, such as the_content() and the_ID() actually echo the returned value, and if applicable will apply the "default filters" for the appropriate values. These functions don't need to be echoed.

echo get_the_ID();

is functionally the same as

the_ID();

If you look at the docs for the_ID() you'll see it literally just outputs the value of get_the_ID(). From the source:

function the_ID() {
    echo get_the_ID();
}

In that vein, if you try and set the_ functions as a variable, you'll leave a trail of echoed variables throughout the page.

$id = the_ID();
echo 'Post ID: '.$id;

will output:

123Post ID: 123

To use get_the_content() and get shortcodes to run, you'll either need to run it through the do_shortcode() function, or better yet the_content filter.

$content = get_the_content();

echo do_shortcode( $content );
// Or:    
echo apply_filters( 'the_content', $content );

If you just need to spit out post content in a template, without any manipulation, you're typically better off with (no echo or echo short tag):

the_content();
like image 111
Xhynk Avatar answered Oct 15 '22 12:10

Xhynk


get_the_content() does not pass the content through the_content. Which means that it does not auto-embed videos, or expand shortcodes, among other things.

Just use get_the_content() and it will remove those tags.

https://codex.wordpress.org/Function_Reference/get_the_content https://developer.wordpress.org/reference/functions/the_content/

like image 37
J Quest Avatar answered Oct 15 '22 12:10

J Quest