webappguides
Create Wordpress Theme From Scratch! Build Your Own

WordPress function to get the post titles

webapps log

webapps Lab
September 29, 2020
ThemeFunctions

get_the_excerpt vs the_excerpt

Every WordPress blog post must have the title, post title shows what the article is all about. Even the google crawler checks the post title to index the page.

In the WordPress framework, there are WordPress functions to get the post titles.

If you are a beginner to the WordPress theme development you may be wondering, what are those WordPress functions for post titles?

the_title and get_the_title functions are the WordPress functions that retrieve the title of a certain current blog post if the blog has the posts. The two functions can receive the parameters of the strings for HTML markup like head tags(<h2>, <h1>), and the post id respectively.

wordpress function to get the title

Though, it is optional to pass the parameters to the_title and get_the_title.

How to use the_title function?

To use the_title to get the title of the post, you should write PHP codes in the HTML source of the single.php and index.php.

<?php the_title();?>

If the blog post title is My first blog post.

The <?php the_title();?> outputs My first blog post.

Place the above code in the suitable HTML elements, always the_title function is used in <title> tag element and head tags (h1, h2, h3).

Here is an example to implement <?php the_title();?> in HTML source.

Starting with the title tag.

<title><?php the_title();?></title>

The output of this code is <title>My first blog post</title>

And in the head tags.

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

The output of this code is <h1>My first blog post</h1>

That is an easy way to populate blog post titles.

But if you haven’t specified the head or title tags in your index.php layout,  you can pass HTML mark up tags in the_title as parameters.

the_title receives three arguments which are

  1. a string $before – an option string to appear before the title text starts, the default value is empty.
  2. string $after – an option string to appear after the title text ends, the default value is empty.
  3. the boolean $echo variable that helps function to echo title text. the default value is true.

Now, it is time to check the usage of $before and $after. I will talk about $echo and its impact later.

Calling the_title function without parameters requires a page to have the title and head tags.

What if you want the function to display the title in the head tag(<h1> and </h1>) automatically.

It is simple to attain that purpose, just pass ‘<h1>’ and ‘</h1>’ in the_title function.

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

If one of the blog titles is My first blog post.

The output is <h1>My first blog post</h1>

How the_title function works?

To understand how the_title function manages to retrieve the title of the current blog post, first, check out the raw source code of the_title function.

YES, it is a good idea to know this as it will give you the freedom to customize its codes.

To find the_title source, go to your WordPress installation folders and files, then open the wp-includes directory.

Inside the wp-includes open a PHP file written post-template.php

the_title function codes start at line number 42. Its complete code is

42. function the_title( $before = '', $after = '', $echo = true ) {
43. $title = get_the_title();

44. if ( strlen( $title ) == 0 ) {
45. return;
46. }

47. $title = $before . $title . $after;

48. if ( $echo ) {
49. echo $title;
50. } else {
51. return $title;
52. }
53. }

the_title function passes three data as I explained earlier, the arguments are for prepending and appending the title.

the_title uses get_the_title to get the title of the current post.

get_the_title returns the text of the post title. The get_the_title function receives one parameter, which is an integer object(post id).

The post id helps a function to get the appropriate post title of the post.

You will learn about the get_the_title in the next section.

The explanation of each code line work.

instance variable $title at Line 43 is set to get_the_title function that brings the string of post title.

For example, if the current title is HELLOW WORLD, get_the_title will make a declaration of  $title = get_the_title() to be $title=’HELLOW WORLD’.

Line 44 checks if the variable $title has no text. The function that counts the length of the text is strlen($title). If there is no string, nothing is returned.

The post title is appended and prepended by $before and $after variables.

So, Line 10 $before variable adds a string at the beginning of the title and $after variable adds a string at the end of the post title.

For example if you have passed the argument of ‘<h1>’ and ‘</h1>’ in the_title, this makes line being $title='<h1>’.$title.'</h1>’

The the_title function checks if the boolean $echo default value(true) hasn’t been changed, if it is true, the_title outputs the title with given settings.

Otherwise, the function returns the title. As you can see in line number 51.

This will change the way you call the_title. that is from <php the_title();?> to <?php echo the_title();?>

What is get_the_title?

get_the_title is an actual WordPress function that retrieves the title of the post. the_title function cannot provide the title without get_the_title.

The get_the_title receives only one parameter, which is an integer or the object of the class WP_Post.

The function uses the WP_Post object to get the ID of the current post for fetching the title.

The question you have right now is, how to use get_the_title in an index.php?

The way the function is used is the same as you call the WordPress function for site URL.

This is how to implement it.

<?php echo get_the_title();?>

So if the current blog post title is MY POST TITLE FOR THIS POST

The output will be MY POST TITLE FOR THIS POST.

To <?php echo get_the_title();?> in the head tags of html elements, write the codes while enclosed by <h1> and </h1>.

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

The post title output now becomes <h1>MY POST TITLE FOR THIS POST</h1>.

How get_the_title works?

get_the_title is able to display titles of the post that were set public and hide the post title that was set protected or private.

get_the_title retrieves the post title by using the post ID. So, the ID is passed as the parameter of the function.

The default post id is zero(0), which is the index of the first post. This index, the post id brings the data of the post.

Among the data post that is being retrieved is the post title.

This code below, it is get_the_title raw source, it shows how get_the_title returns the post title.

1. function get_the_title( $post = 0 ) {
2. $post = get_post( $post );

3. $title = isset( $post->post_title ) ? $post->post_title : '';
4. $id = isset( $post->ID ) ? $post->ID : 0;

5. if ( ! is_admin() ) {
6. if ( ! empty( $post->post_password ) ) {

7. $prepend = __( 'Protected: %s' );

8. $protected_title_format = apply_filters( 'protected_title_format', $prepend, $post );
9. $title = sprintf( $protected_title_format, $title );
10. } elseif ( isset( $post->post_status ) && 'private' === $post->post_status ) {

11. $prepend = __( 'Private: %s' );

13. $private_title_format = apply_filters( 'private_title_format', $prepend, $post );
14. $title = sprintf( $private_title_format, $title );
15. }
16. }

17. return apply_filters( 'the_title', $title, $id );
18. }

The work of each get_the_title code line

Line 1, get_the_title passes one argument, which is the integer id or WP_Post, the default id is zero.

The instance variable $post is set to get_post($post) function (line 2), get_post receive the id passeed by get_the_title.

get_post uses $post id to return the object that is able to retrieve the data of the post. So, $post has now become the object.

$post->post_title retrieves the post title of the given post id(Line 3).

Sometimes the site admin sets a blog post a private, therefore, only readers with password view the post.

wordpress function post title protected

So, Line 5 and Line 6 check if the reader has a password if there is no password Line 10 write post title indicating the post is not public but protected.

Otherwise, if the post is private, Line 14 indicates the post is private.

But if the post is public, Line 17 returns the post title of the given id.

Why the_title over the get_the_title?

I recommend the_title function over get_the_title because the_title allows HTML markups to be its parameter.

get_the_title doesn’t pass three variables, it only receives one parameter which is the post id integer.

get_the_title doesn’t provide a way to manipulate some HTML elements like the_title.

the_title will display the post title without echoing the function while get_the_title has to be echoed to display the post title.

the_title uses get_the_title meaning every work get_the_title can be operated by the_title.

When to use get_the_title?

When you want to display the category(parent) of the current post, get_the_title can show the title of the post’s category.

get_the_title is using get_post($post) which returns all the data relating to the current post. One of the member variable returned is post_parent for the post parent.

$post object for getting post category can be passed in get_the_title as get_the_title($post->post_parent).

Also, you can use get_the_title if you want to display the number of post comments,  to use a function to display the number of comments of the post, write  get_the_title($post->comment_count)

Also, the function can display the excerpt of the post although the_excerpt method is the best function to display post summary.

In short, if you want to manipulate post information get_the_title is a suitable one compared to the_title function.

Wrap-up

There are two WordPress functions to get the post title, the_title, and get_the_title functions. the_title is very suitable when you want to append and prepend  Html elements with the title of the post.

But get_the_title is used when you want to display post title with some additional information relating to the post. The information like the date, author, category, excerpt, number of comments, comments status, and so on can be passed by WP_Post object in get_the_title.

All in all the_title and get_the_title are relating to each other, the_title returns get_the_title meaning that get_the_title does all the processing to retrieve the title. So, you can use any function and you will get the desired results.