首页>建站相关>wp_get_document_title自定义页面title标签

wp_get_document_title自定义页面title标签

因为服务器配置的问题,之前一直用的3.6.1版本的wordpress,眼看这阿里云服务器快要到期,暂时也不准备续费,想把wp博客搬到目前所使用的天翼云服务器上,天翼云的服务器安装的php版本是7.2,尝试了多个wordpress 3.x版本均无法正确识别,于是只能换成了4.5.28版本的wordpress。

这几天一边查百度,一边复制粘贴尝试订制一个自用的主题,才进入head部分就已经举步维艰,看了一下空白页的源代码,3.6.1版本正常生效的tltle标签,在4.5.28版本里变成了空白。

原因是wp_title( )这个函数已经被放弃使用了,自4.4.0版本之后,wordpress提供了新的wp_get_document_title( )函数来生成tltle标签。尝试了一下效果,默认的连接符号也从“|”变成了“-”。

wp_get_document_title()的源码

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 query. */
        $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() && get_queried_object() ) {
        $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() ) {
        /* translators: %s: Page number. */
        $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 ) );

    /**
     * Filters the document title.
     *
     * @since 5.8.0
     *
     * @param string $title Document title.
     */
    $title = apply_filters( 'document_title', $title );

    return $title;
}

标签: wordpress

移动端可扫我直达哦~

推荐阅读

wordpress 2025-03-31

wordpress 重新生成缩略图

测试wp缩略图功能的时候不小心把所有的缩略图都删掉了,但保留了原图,不想一个个重新上传以生成缩略图,就使用了这段代码,注意生成成功之后,这段代码就可以删掉了。function regenerate_all_thumbnails() {...

建站相关 wordpress

wordpress 2025-03-29

WordPress的主循环与WP_Query

WordPress的主循环和WP_Query是主题开发中最重要的两个概念,它们负责从数据库获取内容并显示在页面上。主循环 (The Loop)主循环是WordPress用来显示文章的核心机制。它是一个PHP代码结构,用于遍历当前页面请...

建站相关 wordpress

wordpress 2025-03-22

在phpstudy中为wordpress开启伪静态

原文修改主题都是在服务器上一边在线修改一边调试,用上了phpstudy后才发现自己之前的方式有多没有效率。但测试设置固定链接的时候遇到了一个问题,就是设置前也无风雨也无晴,设置后统一返回404。这个问题之前尝试搭建站点的时候也遇到过,...

建站相关 wordpress

wordpress 2025-03-17

WordPress分页中遇到404错误:posts_per_page

这个问题可能更多为主题开发者所遇见,一款推向市场的主题一般都会几经测试,应该不容易到客户手中才发现这个问题。所以网络上的相关讨论不多,博主也是调试了几天才大致有了一些思路:后台的默认参数在wordpress的后台设置里,是可以设置归档...

建站相关 wordpress

wordpress 2025-03-14

WORDPRESS HEADER模块常用函数

在 WordPress 开发中,header.php 文件是主题的重要组成部分,用于定义网站的头部内容。以下是一些在 header.php 中常用的 WordPress 函数及其用途,如果嫌部分函数生成的默认模板不需要的元素过多,也可...

建站相关 wordpress

wordpress 2025-03-12

wordpress的前后台数据交换ajax

ajax是个耳熟能详的词儿,但因为有点儿复杂,博主一直是规避学习的,今天刚好碰到一个前台jquery向wp后台申请数据的问题。躲不过,那就慢慢调试吧。钩子wp的ajax还区分了用户,对于不同的用户(登录与否)采用不同的钩子,不过这里只...

建站相关 wordpress

wordpress 2025-03-07

WordPress中add_meta_box函数参数详解

add_meta_box 是 WordPress 中用于在后台编辑页面添加自定义元框(Meta Box)的函数。它允许开发者在文章、页面、自定义文章类型等编辑页面中添加自定义字段或内容。以下是 add_meta_box 函数的参数及其...

建站相关 wordpress

wordpress 2025-03-07

wordpress 手动添加自定义字段

自定义字段可以扩展文章的信息,也有很多相关的成熟的插件,比如Advanced Custom Fields (ACF) 插件,如果希望添加的字段不多,也不愿意为此安装过多的插件,我们也可以考虑手动来添加它。为post文章添加字段// 添...

建站相关 wordpress

wordpress 2025-03-07

wordpress 自定义文章类型

在 WordPress 中,Post Type(文章类型)是指不同类型的内容,例如文章(Post)、页面(Page)、自定义文章类型等。每种文章类型可以使用不同的模板来显示其内容。以下是关于 WordPress 文章类型模板的基本信息...

建站相关 wordpress

wordpress 2025-03-06

wordpress媒体上传一张图片裁剪成多张的问题

wordpress媒体上传的时候会自动帮用户处理图片,根据不同的使用场景,它预设了一套通用的缩略图尺寸,媒体中心的方便之处在于,在媒体中心删除图片的时候,wordpress会贴心的把一整套缩略图一起删除掉。但非图片为主的站点,其实并不...

建站相关 wordpress