6

背景:WordPress 5.4.5,Yoast 3.7.1

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

现在,在 StackOverflow 上提出类似问题的其他页面上(例如,此处此处此处),回答者询问是否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 调用并从场外提取内容以包含在页面中。该内容的一部分是要在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 ...

4

3 回答 3

14

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);
于 2016-11-04T05:15:03.653 回答
3

自 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过滤器,它会缩短标题生成,因此如果您不打算对当前标题进行更改,则性能会更高:

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_titlepost_type_archive_title取决于页面的函数生成标题之后,并且在标题标签即将输出之前:

// 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 );
于 2020-06-16T14:29:08.503 回答
0

将此添加到您的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)的优先级更改

于 2016-11-04T05:41:59.097 回答