10

Just a small question. Is there any difference in using

<?php the_title() ?>

or

<?= get_the_title() ?>

Yeah, I know somebody can consider using short echo tag a bad practice, I just want to know is there any difference in result of calling these two functions.

2
  • 1
    why is short echo tag bad practice?
    – Joel M
    Commented Jun 29, 2022 at 15:38
  • short echo tag is excellent practice it removes the superfluous clutter of php end echo
    – th00ht
    Commented Dec 1, 2022 at 10:43

2 Answers 2

12

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.

2
  • 1
    So, technically calling "the_title('', '', false);" would be the same as calling "get_the_title();". Not that I want to use it like that. :)
    – Boykodev
    Commented Dec 16, 2015 at 17:10
  • Yes, that looks right.
    – s_ha_dum
    Commented Dec 16, 2015 at 17:15
3
the_title()

will echo the title for you and can only be used within 'the loop' https://codex.wordpress.org/Function_Reference/the_title

get_the_title()

without the echo or <?= will simply return the title. So you could store it in a variable and manipulate it if you needed to https://codex.wordpress.org/Function_Reference/get_the_title

3
  • So echo get_the_title() has the exactly same result as calling the_title() ?
    – Boykodev
    Commented Dec 16, 2015 at 10:17
  • yes it does. Please see the links above for more information from the docs
    – TommyBs
    Commented Dec 16, 2015 at 10:18
  • That's what I thought, i just had some doubts I wanted to clear.
    – Boykodev
    Commented Dec 16, 2015 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.