single_post_title

Display or retrieve page title for post.

Description

(string|void) single_post_title( (string) $prefix = '', (bool) $display = true ); 

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.

Returns (string|void)

Title when retrieving.

Parameters (2)

0. $prefix — Optional. (string) => ''
What to display before the title.
1. $display — Optional. (bool) => true
Optional, default is true. Whether to display or retrieve title.

Usage

  1. if ( !function_exists( 'single_post_title' ) ) { 
  2. require_once ABSPATH . WPINC . '/general-template.php'; 
  3.  
  4. // Optional. What to display before the title. 
  5. $prefix = ''; 
  6.  
  7. // Optional, default is true. Whether to display or retrieve title. 
  8. $display = true; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = single_post_title($prefix, $display); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/general-template.php  
  1. function single_post_title( $prefix = '', $display = true ) { 
  2. $_post = get_queried_object(); 
  3.  
  4. if ( !isset($_post->post_title) ) 
  5. return; 
  6.  
  7. /** 
  8. * Filters the page title for a single post. 
  9. * @since 0.71 
  10. * @param string $_post_title The single post page title. 
  11. * @param object $_post The current queried object as returned by get_queried_object(). 
  12. */ 
  13. $title = apply_filters( 'single_post_title', $_post->post_title, $_post ); 
  14. if ( $display ) 
  15. echo $prefix . $title; 
  16. else 
  17. return $prefix . $title;