如何更改 WordPress 页面上的标题?

How to change the title on a WordPress page?

上下文:WordPress 5.4.5,Yoast 3.7.1

我是一名插件开发人员,可以访问客户的网站。该站点安装了 Yoast 3.7.1,我想知道这是否重要,因为无论我做什么我都无法更改 404 页面的 title.

现在在 Whosebug 的其他页面上提出了类似的问题(例如 here, here and here),那些回答的人询问 header.php 是否正确嵌入了对 wp_title() 的调用。这是当时当前主题 header.php 中的内容:

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

有趣的是,在我的 404.php 页面中,wp_get_document_title() 告诉我文档标题是 Page not found - XXXX,即使上面的 wp_title 调用将分隔符指定为 |. Yoast 的标题重写已被禁用,所以我完全不确定破折号的来源。

我的插件执行 REST 调用并从 off-site 中提取内容以包含在页面中。该内容的一部分是要在 title 中使用的文本。

在以前的客户端站点上,我能够执行以下操作:

add_filter('wp_title', 'change_404_title');
function change_404_title($title) {
    if (is_404()) 
    {
        global $plugin_title;
        if (!empty($plugin_title)) 
        {
             $title = $plugin_title;
        }
    }
    return $title;
}

但是,在这个网站上,这是行不通的。

我已经尝试过,根据所使用的 WordPress 版本,挂钩 pre_get_document_title 过滤器,即

add_filter('pre_get_document_title', 'change_404_title');

但还是无济于事。我目前正在阅读 Yoast ...

wp_title 自 4.4 版后已弃用。所以我们应该使用新的过滤器pre_get_document_title。您的代码看起来不错,但我对 global $plugin_title 感到困惑。我宁愿请你先试试这个

add_filter('pre_get_document_title', 'change_404_title');
function change_404_title($title) {
    if (is_404()) {
        return 'My Custom Title';
    }
    return $title;
}

如果它不起作用,请尝试更改优先级以最近执行您的功能。

add_filter('pre_get_document_title', 'change_404_title', 50);

将此添加到您的 functions.php

function custom_wp_title($title) {

    if ( is_404() ) {
        $title = 'Custom 404 Title';
    }
    return $title;
}
add_filter( 'wp_title', 'custom_wp_title', 10, 2 );

10 - 优先更改以覆盖其他插件,如 SEO

自 Wordpress v4.4.0 以来,文档标题的生成方式发生了变化。现在 wp_get_document_title 指示如何生成标题:

/**
 * Displays title tag with content.
 *
 * @ignore
 * @since 4.1.0
 * @since 4.4.0 Improved title output replaced `wp_title()`.
 * @access private
 */
function _wp_render_title_tag() {
    if ( ! current_theme_supports( 'title-tag' ) ) {
        return;
    }

    echo '<title>' . wp_get_document_title() . '</title>' . "\n";
}

这是来自 v5.4.2 的代码。这些是您可以用来操作标题标签的过滤器:

function wp_get_document_title() {
    /**
    * Filters the document title before it is generated.
    *
    * Passing a non-empty value will short-circuit wp_get_document_title(),
    * returning that value instead.
    *
    * @since 4.4.0
    *
    * @param string $title The document title. Default empty string.
    */
    $title = apply_filters( 'pre_get_document_title', '' );
    if ( ! empty( $title ) ) {
        return $title;
    }
    // --- snipped ---
    /**
    * Filters the separator for the document title.
    *
    * @since 4.4.0
    *
    * @param string $sep Document title separator. Default '-'.
    */
    $sep = apply_filters( 'document_title_separator', '-' );

    /**
    * Filters the parts of the document title.
    *
    * @since 4.4.0
    *
    * @param array $title {
    *     The document title parts.
    *
    *     @type string $title   Title of the viewed page.
    *     @type string $page    Optional. Page number if paginated.
    *     @type string $tagline Optional. Site description when on home page.
    *     @type string $site    Optional. Site title when not on home page.
    * }
    */
    $title = apply_filters( 'document_title_parts', $title );
    // --- snipped ---
    return $title;
}

所以这里有两种方法可以做到这一点。

第一个使用 pre_get_document_title 过滤器,它 short-circuits 生成标题,因此如果您不打算对当前标题进行更改,则性能更高:

function custom_document_title( $title ) {
    return 'Here is the new title';
}
add_filter( 'pre_get_document_title', 'custom_document_title', 10 );

第二种方法使用 document_title_separatordocument_title_parts 挂钩标题以及稍后在函数中执行的标题分隔符,在使用 single_term_title 或 [= 等函数生成标题之后19=] 取决于页面,并且在即将输出标题标签之前:

// Custom function should return a string
function custom_seperator( $sep ) {
   return '>';
}
add_filter( 'document_title_separator', 'custom_seperator', 10 );

// Custom function should return an array
function custom_html_title( $title ) {
   return array(
     'title' => 'Custom Title',
     'site'  => 'Custom Site'
    );
}
add_filter( 'document_title_parts', 'custom_html_title', 10 );