WordPress function: page title tag acquisition, WP_ get_ document_ title()、wp_ Title () and title tag

The title tag of WordPress is used to define the page title, WP_ get_ document_ title()、wp_ Both title () and title tag can get the title tag of WordPress. But note that as the WordPress version iterates, WP_ Title () has been abandoned. At present, the main function used is WP_ get_ document_ title()。

wp_ get_ document_ title()

Basic usage

<title><?php echo wp_get_document_title(); ?></title>

WordPress version 4.4.0 started, adding a new function WP_ get_ document_ Title (), you only need to add the code above to the topic header.php Before the / head label of.

Advanced Usage

<?php
    function custom_document_title_separator()
    {
        $separator = ' | '; //自定义的分隔符号
        return $separator;
    }
    add_filter('document_title_separator', 'custom_document_title_separator');
 
    function reform_page_title($title)
    {
        $title['tagline'] = 'Site description when on home page.'; //网站描述
        $title['page'] = 'Page number if paginated.';  //分页页码
        $title['site'] = 'Site title when not on home page.';  //不在首页时的网站标题
        return $title;
    }
    add_filter('document_title_parts', 'reform_page_title', 10, 1);
?>

In the theme functions.php Add the above code to customize the separator and modify the generated title title.

matters needing attention

function disableYoastTitleRewrite() {

if (class_ exists('WPSEO_ Frontend') {

$wpseo_ front = WPSEO_ Frontend::get_ instance();

remove_ filter( 'pre_ get_ document_ title', array( $wpseo_ front, 'title' ), 15 );

remove_ filter( 'wp_ title', array( $wpseo_ front, 'title' ), 15 );

}

}

add_ action( 'init', 'disableYoastTitleRewrite');

If you use the yoastSEO plug-in, you need to activate it in the functions.php Add the above code (wpseo (~ 3 +) and WordPress (4.4 +)) to prevent title from being rewritten.

title-tag

WordPress version 4.1.0 begins to add 'title tag' to display the title in the theme to replace the previous WP_ The specific usage of the title() function is as follows:

function add_ theme_ support_ title(){

add_ theme_ support( 'title-tag' );

}

add_ action( 'after_ setup_ theme', 'add_ theme_ support_ title' );

Let's start with the theme functions.php Add the above function.

Then in the topic header file header.php Add the above code to the / head tag (if you already have it, you don't need to add it).

wp_ title()

WordPress 4.4 will discard WP_ Title () function, no longer recommend you to continue, this part of the content is to WP_ Some records of the title () function.

In WP_ Title () is usually in the title element at the head of the page. When WP_ When title () is outside the main loop of the home page, it can be used anywhere in the template.

<?php wp_title( $sep, $echo, $seplocation ); ?> 

Parameter: $SEP (string) (optional) text information (such as separator) before and after the title of the article.

By default (if the separator is set to blank), the @ (?) symbol is displayed before and after the article title (determined by the seplocation parameter). Default:; (?) $echo (Boolean) (optional) response title (true), or return title as PHP string (false).

Default value: true 1 (true) - the default value of 0 (false) $selocation (string) (optional) is introduced into WordPress 2.5, which determines the position of SEP string relative to the title of the article. All values except "right" place SEP in front of the title of the article (to the left).

If the value of seplocation is' right ', the SEP string will be displayed after the title of the article. Default: None

Example 1:

<?php wp_ title( '|', true, 'right' ); ?>

In WordPress Theme header file header.php Add the above code to the / head tag of.

Example 2:

<title>

<?php

if (is_ home()||is_ Search ()) {bloginfo ('name ');} / / the first page and search page display the name of the website, such as Brian

else{wp_ Title ('); echo' | '; bloginfo ('name');} / / other pages display page title and website name, such as an article WordPress function | Brian

?>

</title>

In WordPress Theme header file header.php Add the above code to the / head tag of.

Original article, author: brian, if reprinted, please indicate the source:https://www.nicebrian.com/20.html

Leave a Reply

Your email address will not be published. Required fields are marked *