如何使用 pre_get_document_title 在 WordPress 主题中自定义标题

[ad_1]

ordPress 4.4 及更高版本使用名为 pre_get_document_title() 的过滤器为每个页面、帖子、标签、类别等自定义标题。 例如,我可以使用此过滤器更改 404 错误标题。 可以使用 wp_title() 以前在 4.4 版中已弃用,但由于兼容性原因而返回。

让我们看看如何更改或覆盖页面标题。 我正在使用二十六儿童主题。

语法和示例

语法非常简单:

// add filter
add_filter('pre_get_document_title', 'change_my_title');
// Our function
function change_my_title($title) {
     return 'My new title';
}

以上将更改所有页面/帖子/标签等的标题。 所以你必须根据你的需要定制它。 例如,只需更改 404 页面标题:

// add filter
add_filter('pre_get_document_title', 'change_my_404_title');
// Our function
function change_my_404_title($title) {
     // checks if 404 error is being displayed
     if ( is_404() ) {
       return 'Error 404: Oh no. Page note found :(';
     }
     // else return default title
     return $title;
}

以上 允许 pre_get_document_title 过滤器 我根据我的需要覆盖任何 HTTP 404 错误页面标题。 这是我为自定义分类法自定义标题的另一个示例:

add_filter('pre_get_document_title', 'change_faq_tax_title');
function change_faq_tax_title($title) {
    global $page, $paged;
    // if Easy taxonomy 
    if (is_tax('tutoriallevel', 'easy')) {
        $nix_title = single_term_title( '', false ) . ' to follow Linux/Unix tutorials for new users - nixCraft ';
        // Add a page number if necessary.
        if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
                $nix_title .= sprintf( __( 'Page %s' ), max( $paged, $page ) );
        }
        return $nix_title;
    }
}

你必须把上面的代码放在 位于您的子主题中的functions.php. 传递非空值将使 wp_get_document_title() 短路,而是返回该值。 wp_get_document_title() 返回当前页面/标签/帖子的文档标题。 这是一个原始函数取自 WordPress核心文件

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;
    }
 
    global $page, $paged;
 
    $title = array(
        'title' => '',
    );
 
    // If it's a 404 page, use a "Page not found" title.
    if ( is_404() ) {
        $title['title'] = __( 'Page not found' );
 
    // If it's a search, use a dynamic search results title.
    } elseif ( is_search() ) {
        /* translators: %s: search phrase */
        $title['title'] = sprintf( __( 'Search Results for “%s”' ), get_search_query() );
 
    // If on the front page, use the site title.
    } elseif ( is_front_page() ) {
        $title['title'] = get_bloginfo( 'name', 'display' );
 
    // If on a post type archive, use the post type archive title.
    } elseif ( is_post_type_archive() ) {
        $title['title'] = post_type_archive_title( '', false );
 
    // If on a taxonomy archive, use the term title.
    } elseif ( is_tax() ) {
        $title['title'] = single_term_title( '', false );
 
    /*
     * If we're on the blog page that is not the homepage or
     * a single post of any post type, use the post title.
     */
    } elseif ( is_home() || is_singular() ) {
        $title['title'] = single_post_title( '', false );
 
    // If on a category or tag archive, use the term title.
    } elseif ( is_category() || is_tag() ) {
        $title['title'] = single_term_title( '', false );
 
    // If on an author archive, use the author's display name.
    } elseif ( is_author() && $author = get_queried_object() ) {
        $title['title'] = $author->display_name;
 
    // If it's a date archive, use the date as the title.
    } elseif ( is_year() ) {
        $title['title'] = get_the_date( _x( 'Y', 'yearly archives date format' ) );
 
    } elseif ( is_month() ) {
        $title['title'] = get_the_date( _x( 'F Y', 'monthly archives date format' ) );
 
    } elseif ( is_day() ) {
        $title['title'] = get_the_date();
    }
 
    // Add a page number if necessary.
    if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
        $title['page'] = sprintf( __( 'Page %s' ), max( $paged, $page ) );
    }
 
    // Append the description or site title to give context.
    if ( is_front_page() ) {
        $title['tagline'] = get_bloginfo( 'description', 'display' );
    } else {
        $title['site'] = get_bloginfo( 'name', 'display' );
    }
 
    /**
     * 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 );
 
    $title = implode( " $sep ", array_filter( $title ) );
    $title = wptexturize( $title );
    $title = convert_chars( $title );
    $title = esc_html( $title );
    $title = capital_P_dangit( $title );
 
    return $title;
}

我希望有人会发现这很有用,因为我花了一个小时来找出新的变化。 大多数论坛帖子已经过时或谈论修改核心文件。 因此我写了一篇快速的博客文章。

[ad_2]

Related Posts