描述
默认情况下,页面标题将在标题之前显示分隔符,以便博客标题位于页面标题之前。这不利于标题显示,因为博客标题显示在大多数选项卡上,而不是用户正在查看的页面。
将博客标题放在页面标题的后面或“右边”也有SEO的好处。然而,在大多数浏览器支持标签的情况下,将博客标题放在右边是常识。您可以通过使用seplocation参数并将值设置为“right”来实现这一点。这一变化是在2.5.0左右引入的,以防主题的向后兼容性很重要。
参数
- $sep
-
(string) (可选) 如何分隔页面标题中的各项内容,默认值“»”。
默认值: '»'
- $display
-
(bool) (可选) 是显示还是检索标题。
默认值: true
- $seplocation
-
(string) (可选) 分隔符的位置(“left”或“right”)。
默认值: ''
返回
(string|void) 如果 $display
为 false,返回字符串,否则无返回。
说明
最佳实践
插件可以使用wp_title过滤器生成值。虽然可以通过与bloginfo
(网站名称)连接等方式构造“标题”,但如果在主题中不使用wp_title
函数,则可能会出现错误或意外行为。
返回值
该函数返回一个串联字符串,它总是查询数据库中的默认字符串,默认字符串的值取决于文章或页面的类型:
- 文章单页
- 文章标题
- 基于日期的存档
- 日期 (如 “2006”, “2006 – January”)
- 类别
- 类别的名称
- 作者页
- 用户的公开名称
然后,该函数在sep字符串的前面或后面追加,并返回整个值。
源码
更新日志
版本 | 描述 |
---|---|
1.0.0 | 开始引入 |
使用示例
以下是建议使用的方法,而不是wp_title()函数。
/* * Let WordPress manage the document title. * By adding theme support, we declare that this theme does not use a * hard-coded <title> tag in the document head, and expect WordPress to * provide it for us. */ add_theme_support( 'title-tag' );
它应该放在 functions.php 中,带有
after_setup_theme
动作钩子。wp_title() 最初在4.4版本中被废弃,但在努力确定使用该函数的意外方式以确保弃用不会破坏它们时,又被恢复了。由于 wp_title() 有可能在不久的将来被废弃,我们建议你改用它的替代品:title-tag 主题功能。
零字符分隔符
sep字符串可以是零字符,这将从返回值中删除 » 要做到这一点,请将sep参数设置为零字符,例如:<title><?php wp_title(''); ?></title>
如果文章的标题是“Hello world!”,然后该函数将返回:
Hello world!
使用过滤器自定义
如果您想更改标题在不同页面上的显示方式,可以使用过滤器。/** * Filters wp_title to print a neat <title> tag based on what is being viewed. * * @param string $title Default title text for current view. * @param string $sep Optional separator. * @return string The filtered title. */ function wpdocs_theme_name_wp_title( $title, $sep ) { if ( is_feed() ) { return $title; } global $page, $paged; // Add the blog name $title .= get_bloginfo( 'name', 'display' ); // Add the blog description for the home/front page. $site_description = get_bloginfo( 'description', 'display' ); if ( $site_description && ( is_home() || is_front_page() ) ) { $title .= " $sep $site_description"; } // Add a page number if necessary: if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) { $title .= " $sep " . sprintf( __( 'Page %s', '_s' ), max( $paged, $page ) ); } return $title; } add_filter( 'wp_title', 'wpdocs_theme_name_wp_title', 10, 2 );
覆盖主页
如果您使用带有自定义循环和内容的自定义主页或自定义首页,您将拥有空的wp_title
。在主页的wp_title
位置添加描述/标语的巧妙技巧如下:<title><?php wp_title(''); ?></title>
然后在主题的
functions.php
文件中:add_filter( 'wp_title', 'wpdocs_hack_wp_title_for_home' ); /** * Customize the title for the home page, if one is not set. * * @param string $title The original title. * @return string The title to use. */ function wpdocs_hack_wp_title_for_home( $title ) { if ( empty( $title ) && ( is_home() || is_front_page() ) ) { $title = __( 'Home', 'textdomain' ) . ' | ' . get_bloginfo( 'description' ); } return $title; }
当然,您可以根据需要格式化此字符串。
默认
如果没有为单个文章传递任何参数,例如:<title><?php wp_title(); ?></title>
如果文章的标题是“Hello world!”,然后函数将返回
» Hello world!
只是想检查833行是否拼写错误:
$t_sep = ‘%WP_TITILE_SEP%’; // 临时分隔器,用于准确翻转(如有必要)
WP_TITILE_SEP 拼写正确吗?我可能以为是 WP_TITLE_SEP
我在我的WP php文件中注意到了这一点,然后再次在这里。
在页面/文章标题后显示博客标题:
<title><?php wp_title(''); echo ' | '; bloginfo( 'name' ); ?></title>
为了解决主页的空标题标签问题,我在标题下面使用了这段代码,在 header.php 模板:
<title> <?php if(is_front_page() || is_home()){ echo get_bloginfo('name'); } else{ echo wp_title(''); }?> </title>
对于设置POST_NAME | BLOG_NAME,这一行代码应该很棒
<?php wp_title('|',true,'right'); ?> <?php bloginfo('name'); ?>
要解决主页空标题标签,请在标题上使用此代码 header.php :
<?php echo is_front_page() ? get_bloginfo( 'name' ) : wp_title( '' ); ?>