1

Is there any difference in using

<?php get_the_title(); ?>

or

<?php single_post_title(); ?>

Because I can always get same value

2

single_post_title() and get_the_title() work completely differently.

get_the_title() (or the_title(), which works the same) will get the title for the current post in the loop:

<?php 
if ( have_posts() ) : 
    while ( have_posts() ) : the_post(); 
        the_title();
    endwhile; 
endif; 
?>

So it's the function to use to get the post title for each post in the template for the blog and archives, when used inside the loop. get_the_title() can also be used to get the title for a specific post. This can be done by passing the ID to the post whose title you want:

echo get_the_title( 123 );

single_post_title(), on the other hand, gets the title of the queried object. When you are on a single post or page, the "queried object" will be a WP_Post representing that post or page, and single_post_title() gets the title of that post. I found this article which describes what the queried object is in more detail.

So on a single template this will almost certainly be the same as the current post in the loop. However, because this function always returns the title of the queried object, you can use it to get the title of the current page outside of the loop, or inside a secondary loop.

Also note, that because the queried object of the blog and archives isn't a single post or page, single_post_title() will not work on those pages. For those pages you want to use the_archive_title(), or for taxonomy archives, single_term_title().

4
  • so I think single_post_title() and get_the title() are available without passing ID if I wanna display current page outside of the loop, or inside a secondary loop. .Then what is the difference? still I dont understand – Kazu25 Mar 13 '20 at 10:06
  • I’ve explained the difference in a fair amount of detail. What’s not clear? – Jacob Peattie Mar 13 '20 at 11:03
  • As I mentioned above, Your explanation conclusion is single_post_title() and get_the title() are available outside of loop. So The difference is how to retrieve the WP_Post Object( get_queried_object() for single_post_title() , get_post() for get_the_title()). You mean this right? Actually I understood this But I wanna know How to use them differently depending on the senario or situation – Kazu25 Mar 13 '20 at 17:40
  • There’s rarely a good reason to use single_post_title() – Jacob Peattie Mar 14 '20 at 3:25
1

Yes, there are some differences in the two. Lets take a look.

single_post_title()

This is optimized for single.php template file for displaying the post title.

It does not support placing the separator after the title, but by leaving the prefix parameter empty, you can set the title separator manually. The prefix does not automatically place a space between the prefix, so if there should be a space, the parameter value will need to have it at the end.

Parameters

$prefix

(string) (Optional) What to display before the title.

Default value: ''

$display

(bool) (Optional) Whether to display or retrieve title.

Default value: true


get_the_title()

If the post is protected and the visitor is not an admin, then "Protected" will be displayed before the post title. If the post is private, then "Private" will be located before the post title.

Parameters

$post

(int|WP_Post) (Optional) Post ID or WP_Post object.

Default is global $post.


To be honest I didn't even know there was a single_post_title(). I just use get_the_title() or the_title() depending on if I want the value echoed or not. These can be used anywhere, either by using the global post or passing an ID.

0

Both are different.

  1. get_the_title( int|WP_Post $post ) - Retrieve post title.

If the post is protected and the visitor is not an admin, then "Protected" will be displayed before the post title. If the post is private, then "Private" will be located before the post title.

  1. single_post_title( string $prefix = '', bool $display = true ) - Display or retrieve page title for post.

This is optimized for single.php template file for displaying the post title.

It does not support placing the separator after the title, but by leaving the prefix parameter empty, you can set the title separator manually. The prefix does not automatically place a space between the prefix, so if there should be a space, the parameter value will need to have it at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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