webappguides
Create Wordpress Theme From Scratch! Build Your Own

Differences between the_title vs get_the_title

webapps log

webapps Lab
October 25, 2020
ThemeFunctions

get_the_excerpt vs the_excerpt

This post is going to reveal some differences between the_title and get_the_title.

When you call the_title, the result is not different from get_the_title.

And I’m sure you also have been looking for the_title vs get_the_title like me.

The functions have similarities, but they differ.

The question is, how are they different?

WordPress the_title function retrieves the title of the current post. It is able to fetch the post with an optional HTML marker up. The marker up can be passed as string parameters of the the_title() but get_the_title() retrieves the post title using optional post id, the id of the specific is passed as a parameter, get_the_title default post id is zero means most current post title.

the_title uses get_the_title to echo the post main title.

Therefore, when the_title is implemented in PHP codes, the function will display the post title without echoing.

But you must echo get_the_title if it is called as this function returns only the value of the post title.

the_title vs get_the_title : other key differences

# the_title() get_the_title()
1 the_title() fetches the post title without echoing the function, for example

<?php the_title();?>

get_the_title requires echoing to display the title of the post, for example <?php echo get_the_title();?>
2 Receives three optional parameters, the first two parameters are string optional parameters eg <?php the_title(‘<h1>’,'</h1>’);?> and the third is boolean. Receives only one optional parameter, which is the number of the post (post id) or WP_Post object to get other data of the post like the title of the parent post.
3 It is recommended to use the_title() in the loop get_the_title is useful outside the loop to retrieve a single post

The two functions relating to each other.

In order to know the actual differences between the_title and get_the_title is to look at the proper usage of each function.

How each function is supposed to be implemented?

Let’s start with . . . . .

get_the_title usages

Now you know the get_the_title() argument can be WP_Post object or the integer that represents the id of the post.

Assume, you want to get the post title number 15.

To use get_the_post to get the title of the post, pass the number 15 as a parameter.

Like this,

<?php echo get_the_title(15);?>

So, if the post title of post number 15 is “HELLOW WEBAPPGUIDES”

You will see “HELLOW WEBAPPGUIDES” on your page.

Don’t write this way inside the while as it will rewrite the same post title many times.

But how WP_Post is used in get_the_title?

WP_Post can be used to retrieve the ID, the title of the parent post.

So, you can use the WP_Post global $post object to display the modification date of the post.

For example,

<?php echo get_the_title($post->post_modified);?>

Other usages of the get_the_title are

  1. The excerpt of the current post title.
  2. The title of the current post title
  3. Post type
  4. Number of the comment of the current post title

Reference: https://developer.wordpress.org/reference/classes/wp_post/

the_title usages

Writing the_title without passing parameters displays the title of the current post.

For example,

<?php the_title();?>

But the function can receive three parameters, which are two strings and the boolean value.

Passing these arguments in the the_title function adds the usages.

For example,

If you want to set the title the heading level 1, pass two string parameters of ‘<h1>’ and ‘</h1>’ like this …,

<?php the_title('<h1>','</h1>');?>

There is another to note when you want to add a third argument which is boolean.

The default value of the the_title third parameter is TRUE.

Changing this to FALSE will make the_title to print post title by echoing.

<?php echo the_title(false);?>

When to use the_title

Use the_title when you want to display all posts of the blog.

This will require a loop, specifically a while loop.

Therefore, the_title is supposed to be written inside the loop.

What will happen if you call the_title outside the loop?

the_title will output only one post title, the most current one. So, you won’t be able to retrieve other titles of the posts.

And also, the_title doesn’t pass the post ID to get the title, so you can’t select a specific post title to display it somewhere on the blog page.

Hence, in order to manipulate the_title, you need a loop.

Here, it is an example of implementing the_title() inside a loop.

1. <?php if(has_post()): the_post();?>

2. <?php while(has_posts()):?>

3.<h1><?php the_title();?></h1>

Or you can change line number 3 by passing the strings of <h1> and </h1>

3.<?php the_title('<h1>','</h1>');?>

Learn more about the wordpress functions to get the post title.

When to use get_the_title

get_the_title can be used within a loop or outside the loop.

When you implement this function inside the loop, you should not pass in the number of the post (post id).

Because passing the integer in get_the_title repeats the same post many times.

Instead, this function should be called like this ….,

2. <?php while(has_posts()):?>

3.<h1><?php echo get_the_title();?></h1>

You should echo get_the_title() to display the post title as I did at line number 3.

When you want to display the title of a specific post, get_the_title can be used.

What you are required is to pass the number(integer) of the post (post id).

For example, if I want to display the title of post number 14 on the showcase page (landing page).

You can use get_the_title then passing the post number (post id).

<?php echo get_the_title(14);?>

And, if you want to get the parent page.

<?php echo get_the_title($post->post_parent);?>

That can be used if the post has the parent’s post.

Wrap-up

From what I have seen through reading various discussions on the forums, blogs, and WordPress developer official website, the main difference between the_title and get_the_title is echoing. the_title is echoing a title automatically but you need to echo get_the_title to fetch the title of a given post.

the_title is echoing get_the_title value, this means the_title takes the title value of the post from the get_the_title.

Though get_the_title is more complex as it can receive the id of the post and WP_Post object which gives you the opportunity to retrieve some additional information of the current post title, for example, passing global $post object can retrieve the parent post, the excerpt, the date in which a post title was changed and many other data, check out about WP_Post here.