Is there any difference between the_title() and echo get_the_title()?

The two are not 100% identical, though they are close.

  1. the_title() will echo content by default but the third
    parameter can be used to change that default.
  2. the_title() prepends the optional $before and appends the
    optional $after arguments. If theme or plugin code uses these
    arguments the output of the two functions will be different.

If you take a look at the source, the differences are easy to spot:

32  /**
33   * Display or retrieve the current post title with optional content.
34   *
35   * @since 0.71
36   *
37   * @param string $before Optional. Content to prepend to the title.
38   * @param string $after  Optional. Content to append to the title.
39   * @param bool   $echo   Optional, default to true.Whether to display or return.
40   * @return string|void String if $echo parameter is false.
41   */
42  function the_title( $before="", $after="", $echo = true ) {
43          $title = get_the_title();
44  
45          if ( strlen($title) == 0 )
46                  return;
47  
48          $title = $before . $title . $after;
49  
50          if ( $echo )
51                  echo $title;
52          else
53                  return $title;
54  }

You can see that the_title() pulls data using get_the_title() on its first line, so at that point the two are the same. But the_title() then does additional manipulation, potentially.

The same is true of some of the other “echo”/”not echo” functions such as the_content() and get_the_content(). While close, they are not exactly the same.

Leave a Comment