最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Is there any difference between get_the_title() and single_post_title()?

programmeradmin3浏览0评论

Is there any difference in using

<?php get_the_title(); ?>

or

<?php single_post_title(); ?>

Because I can always get same value

Is there any difference in using

<?php get_the_title(); ?>

or

<?php single_post_title(); ?>

Because I can always get same value

Share Improve this question asked Mar 8, 2020 at 10:54 Kazu25Kazu25 331 silver badge7 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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().

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.

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.

发布评论

评论列表(0)

  1. 暂无评论