wp_title

Display or retrieve page title for all areas of blog.

Description

(string|null) wp_title( (string) $sep = '»', (constant) $display = true, (string) $seplocation = '' ); 

By default, the page title will display the separator before the page title, so that the blog title will be before the page title. This is not good for title display, since the blog title shows up on most tabs and not what is important, which is the page that the user is looking at.

There are also SEO benefits to having the blog title after or to the right of the page title. However, it is mostly common sense to have the blog title to the right with most browsers supporting tabs. You can achieve this by using the seplocation parameter and setting the value to right. This change was introduced around 2.5.0, in case backward compatibility of themes is important.

Returns (string|null)

String on retrieve, null when displaying.

Parameters (3)

0. $sep — Optional. (string) => '»'
Optional, default is ».. How to separate the various items within the page title.
1. $display — Optional. (constant) => true
Optional, default is true. Whether to display or retrieve title.
2. $seplocation — Optional. (string) => ''
Direction to display title, right..

Usage

  1. if ( !function_exists( 'wp_title' ) ) { 
  2. require_once ABSPATH . WPINC . '/general-template.php'; 
  3.  
  4. // Optional, default is '»'. How to separate the various items 
  5. // within the page title. 
  6. $sep = '»'; 
  7.  
  8. // Optional, default is true. Whether to display or retrieve title. 
  9. $display = true; 
  10.  
  11. // Optional. Direction to display title, 'right'. 
  12. $seplocation = ''; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = wp_title($sep, $display, $seplocation); 
  16.  

Defined (1)

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

/wp-includes/general-template.php  
  1. function wp_title( $sep = '»', $display = true, $seplocation = '' ) { 
  2. global $wp_locale; 
  3.  
  4. $m = get_query_var( 'm' ); 
  5. $year = get_query_var( 'year' ); 
  6. $monthnum = get_query_var( 'monthnum' ); 
  7. $day = get_query_var( 'day' ); 
  8. $search = get_query_var( 's' ); 
  9. $title = ''; 
  10.  
  11. $t_sep = '%WP_TITLE_SEP%'; // Temporary separator, for accurate flipping, if necessary 
  12.  
  13. // If there is a post 
  14. if ( is_single() || ( is_home() && ! is_front_page() ) || ( is_page() && ! is_front_page() ) ) { 
  15. $title = single_post_title( '', false ); 
  16.  
  17. // If there's a post type archive 
  18. if ( is_post_type_archive() ) { 
  19. $post_type = get_query_var( 'post_type' ); 
  20. if ( is_array( $post_type ) ) { 
  21. $post_type = reset( $post_type ); 
  22. $post_type_object = get_post_type_object( $post_type ); 
  23. if ( ! $post_type_object->has_archive ) { 
  24. $title = post_type_archive_title( '', false ); 
  25.  
  26. // If there's a category or tag 
  27. if ( is_category() || is_tag() ) { 
  28. $title = single_term_title( '', false ); 
  29.  
  30. // If there's a taxonomy 
  31. if ( is_tax() ) { 
  32. $term = get_queried_object(); 
  33. if ( $term ) { 
  34. $tax = get_taxonomy( $term->taxonomy ); 
  35. $title = single_term_title( $tax->labels->name . $t_sep, false ); 
  36.  
  37. // If there's an author 
  38. if ( is_author() && ! is_post_type_archive() ) { 
  39. $author = get_queried_object(); 
  40. if ( $author ) { 
  41. $title = $author->display_name; 
  42.  
  43. // Post type archives with has_archive should override terms. 
  44. if ( is_post_type_archive() && $post_type_object->has_archive ) { 
  45. $title = post_type_archive_title( '', false ); 
  46.  
  47. // If there's a month 
  48. if ( is_archive() && ! empty( $m ) ) { 
  49. $my_year = substr( $m, 0, 4 ); 
  50. $my_month = $wp_locale->get_month( substr( $m, 4, 2 ) ); 
  51. $my_day = intval( substr( $m, 6, 2 ) ); 
  52. $title = $my_year . ( $my_month ? $t_sep . $my_month : '' ) . ( $my_day ? $t_sep . $my_day : '' ); 
  53.  
  54. // If there's a year 
  55. if ( is_archive() && ! empty( $year ) ) { 
  56. $title = $year; 
  57. if ( ! empty( $monthnum ) ) { 
  58. $title .= $t_sep . $wp_locale->get_month( $monthnum ); 
  59. if ( ! empty( $day ) ) { 
  60. $title .= $t_sep . zeroise( $day, 2 ); 
  61.  
  62. // If it's a search 
  63. if ( is_search() ) { 
  64. /** translators: 1: separator, 2: search phrase */ 
  65. $title = sprintf( __( 'Search Results %1$s %2$s' ), $t_sep, strip_tags( $search ) ); 
  66.  
  67. // If it's a 404 page 
  68. if ( is_404() ) { 
  69. $title = __( 'Page not found' ); 
  70.  
  71. $prefix = ''; 
  72. if ( ! empty( $title ) ) { 
  73. $prefix = " $sep "; 
  74.  
  75. /** 
  76. * Filters the parts of the page title. 
  77. * @since 4.0.0 
  78. * @param array $title_array Parts of the page title. 
  79. */ 
  80. $title_array = apply_filters( 'wp_title_parts', explode( $t_sep, $title ) ); 
  81.  
  82. // Determines position of the separator and direction of the breadcrumb 
  83. if ( 'right' == $seplocation ) { // sep on right, so reverse the order 
  84. $title_array = array_reverse( $title_array ); 
  85. $title = implode( " $sep ", $title_array ) . $prefix; 
  86. } else { 
  87. $title = $prefix . implode( " $sep ", $title_array ); 
  88.  
  89. /** 
  90. * Filters the text of the page title. 
  91. * @since 2.0.0 
  92. * @param string $title Page title. 
  93. * @param string $sep Title separator. 
  94. * @param string $seplocation Location of the separator (left or right). 
  95. */ 
  96. $title = apply_filters( 'wp_title', $title, $sep, $seplocation ); 
  97.  
  98. // Send it out 
  99. if ( $display ) { 
  100. echo $title; 
  101. } else { 
  102. return $title;