wp_title()
Display or retrieve page title.
The function is deprecated from WP 4.4.
Instead, you should use theme support 'title-tag':
/* * Let WordPress manage the document title. * By adding theme support, we declare that this theme does not use a * hard-coded <title> tag in the document head, and expect WordPress to * provide it for us. */ add_theme_support( 'title-tag' );
'title-tag' works base on wp_get_document_title().
You can specify a separator between a page title and additional words in the title. Since 2.5, you can specify the direction of the separator: right, left (by default, no need to specify it).
This template tag can be used anywhere: inside The Loop and outside of it. Usually, it's used to display the <title>
tag in the <head>
of the page.
Hooks from the function
Return
String|null
. String when $display is false, nothing otherwise.
Usage
wp_title( $sep, $display, $seplocation );
- $sep(string)
- How to separate the various items within the page title.
Default: '»' - $display(true/false)
- Whether to display or retrieve title.
Default: true - $seplocation(string)
- Direction to display a separator. Introduced in version 2.5. If you specify right, the separator will be located to the right of the title, in all other cases, it will be to the left.
Default: ''
Examples
#1 Without a separator
<title><?php bloginfo('name'); ?> <?php wp_title('', true); ?></title>
#2 Display for homepage
If for the homepage we use a custom template with an arbitrary output (The Loop), then the page title may be incorrect. To avoid this, use a similar code:
<title><?php bloginfo('name'); ?> | <?php is_home() ? bloginfo('description') : wp_title(''); ?></title>
#3 Using a custom separator
Use blog name before page title, and use |
as a separator.
<title><?php bloginfo('name'); ?><?php wp_title('|'); ?></title>
Output: Blog title | Page title
Important! Don't use _
and --
as separators, it may cause errors.
#4 Reverse the titles
If you want to display the page title first, then the blog title use the following code:
<title><?php wp_title('|', true, 'right'); ?><?php bloginfo('name'); ?></title>
#5 Display the blog title
<title><?php bloginfo('name'); ?> <?php wp_title(); ?></title>
Output: Blog title » Page title
Notes
- Global. WP_Locale. $wp_locale WordPress date and time locale object.
Changelog
Since 1.0.0 | Introduced. |