WordPress函数wp_title()

2.9k 记录 5评论

wp_title() 用来获取当前网页的标题,也就是 title 标签里边的内容。

wp_title() 能在不同的页面自动生成不同的标题(比如首页就是网站标题、文章页就是文章标题)。WordPress 官方的主题都在使用这个函数生成标题,但是在国内的主题中却总是被忽视(因为默认情况下这个函数对 SEO 并不是太好)。

我还是推荐使用这个函数调用标题,更加符合主题开发规范,如果你要让他变的更加符合 SEO,可以使用过滤器优化一下,本文结尾有优化方法。

用法

定义

wp_title( $sep, $display, $seplocation );

参数

  • $sep:(字符串)(可选)标题内容的分隔符,一般设置成 “|” 或者 “-”。默认值:»(»)。
  • $display:(布尔)(可选)是否直接打印标题,如果设置成 False 则返回标题,可以存储到变量里。默认值:True(直接打印输出)。
  • $seplocation:(字符串)(可选)分隔符所在位置,左边还是右边,如果传递 “right” 则为右边,其它任何内容都是左边。默认值:空字符串(左边)

返回值(字符串)

如果 $display 参数设置成 False,才能返回标题的内容,根据不同的标题会返回不同的内容,默认情况下返回的内容是这样的:

文章页:文章标题
日期页:日期
分类页:分类标题
作者页:作者名字

源码

如果想了解更详细,可参考该函数的源码,此函数位于:wp-includes/general-template.php 。

/**
 * Display or retrieve page title for all areas of blog.
 *
 * By default, the page title will display the separator before the page title,
 * so that the blog title will be before the page title. This is not good for
 * title display, since the blog title shows up on most tabs and not what is
 * important, which is the page that the user is looking at.
 *
 * There are also SEO benefits to having the blog title after or to the 'right'
 * or the page title. However, it is mostly common sense to have the blog title
 * to the right with most browsers supporting tabs. You can achieve this by
 * using the seplocation parameter and setting the value to 'right'. This change
 * was introduced around 2.5.0, in case backwards compatibility of themes is
 * important.
 *
 * @since 1.0.0
 *
 * @param string $sep Optional, default is '»'. How to separate the various items within the page title.
 * @param bool $display Optional, default is true. Whether to display or retrieve title.
 * @param string $seplocation Optional. Direction to display title, 'right'.
 * @return string|null String on retrieve, null when displaying.
 */
function wp_title($sep = '»', $display = true, $seplocation = '') {
    global $wp_locale;
 
    $m = get_query_var('m');
    $year = get_query_var('year');
    $monthnum = get_query_var('monthnum');
    $day = get_query_var('day');
    $search = get_query_var('s');
    $title = '';
 
    $t_sep = '%WP_TITILE_SEP%'; // Temporary separator, for accurate flipping, if necessary
 
    // If there is a post
    if ( is_single() || ( is_home() && !is_front_page() ) || ( is_page() && !is_front_page() ) ) {
        $title = single_post_title( '', false );
    }
 
    // If there's a post type archive
    if ( is_post_type_archive() ) {
        $post_type = get_query_var( 'post_type' );
        if ( is_array( $post_type ) )
            $post_type = reset( $post_type );
        $post_type_object = get_post_type_object( $post_type );
        if ( ! $post_type_object->has_archive )
            $title = post_type_archive_title( '', false );
    }
 
    // If there's a category or tag
    if ( is_category() || is_tag() ) {
        $title = single_term_title( '', false );
    }
 
    // If there's a taxonomy
    if ( is_tax() ) {
        $term = get_queried_object();
        if ( $term ) {
            $tax = get_taxonomy( $term->taxonomy );
            $title = single_term_title( $tax->labels->name . $t_sep, false );
        }
    }
 
    // If there's an author
    if ( is_author() && ! is_post_type_archive() ) {
        $author = get_queried_object();
        if ( $author )
            $title = $author->display_name;
    }
 
    // Post type archives with has_archive should override terms.
    if ( is_post_type_archive() && $post_type_object->has_archive )
        $title = post_type_archive_title( '', false );
 
    // If there's a month
    if ( is_archive() && !empty($m) ) {
        $my_year = substr($m, 0, 4);
        $my_month = $wp_locale->get_month(substr($m, 4, 2));
        $my_day = intval(substr($m, 6, 2));
        $title = $my_year . ( $my_month ? $t_sep . $my_month : '' ) . ( $my_day ? $t_sep . $my_day : '' );
    }
 
    // If there's a year
    if ( is_archive() && !empty($year) ) {
        $title = $year;
        if ( !empty($monthnum) )
            $title .= $t_sep . $wp_locale->get_month($monthnum);
        if ( !empty($day) )
            $title .= $t_sep . zeroise($day, 2);
    }
 
    // If it's a search
    if ( is_search() ) {
        /* translators: 1: separator, 2: search phrase */
        $title = sprintf(__('Search Results %1$s %2$s'), $t_sep, strip_tags($search));
    }
 
    // If it's a 404 page
    if ( is_404() ) {
        $title = __('Page not found');
    }
 
    $prefix = '';
    if ( !empty($title) )
        $prefix = " $sep ";
 
    /**
     * Filter the parts of the page title.
     *
     * @since 4.0.0
     *
     * @param array $title_array Parts of the page title.
     */
    $title_array = apply_filters( 'wp_title_parts', explode( $t_sep, $title ) );
 
     // Determines position of the separator and direction of the breadcrumb
    if ( 'right' == $seplocation ) { // sep on right, so reverse the order
        $title_array = array_reverse( $title_array );
        $title = implode( " $sep ", $title_array ) . $prefix;
    } else {
        $title = $prefix . implode( " $sep ", $title_array );
    }
 
    /**
     * Filter the text of the page title.
     *
     * @since 2.0.0
     *
     * @param string $title       Page title.
     * @param string $sep         Title separator.
     * @param string $seplocation Location of the separator (left or right).
     */
    $title = apply_filters( 'wp_title', $title, $sep, $seplocation );
 
    // Send it out
    if ( $display )
        echo $title;
    else
        return $title;
 
}

很显然默认情况下标题是比较简陋的,对 SEO 并不是很友好。

使用和优化

在网站模板中可以这样使用:

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

如果要进行优化,可以使用 wp_title 过滤器定制,具体参考《WordPress通过functions.php修改meta title样式》。

 

5条评论

奶爸建网站笔记 says: 回复

博主请教一个问题,我现在用的主题就是使用的这个函数,但是首页分页的话不会显示第二页第三页这种,分类和TAG又会显示,应该怎么修复这个bug?

大庆seo says: 回复

代码拿走了,感谢分享

云点 says: 回复

超级详细,有用

巴图鲁 says: 回复

Wodpress首页短代码调用最新的博客,有啥好办法啊?

O
OTLASER says: 回复

WordPress基本上是最流行的建站程序了,有时也稍微要懂点这些知识才行,感谢楼主

回复 巴图鲁 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

昵称 *