就在刚刚,WordPress 推送了 4.4 正式版,可以在后台进行升级。对于主题开发者来说,需要注意的是:官方已经不再推荐使用 wp_title() 函数来添加网页标题,而是将网页标题变成了一个主题功能。
以前
以前,带有网页标题的 header.php 文件应该是类似这样的,在一对 <title> 标签中调用 wp_title() 函数。
|
<!DOCTYPE html> <html> <head> <title><?phpwp_title('|',true,'right');?></title> <?phpwp_head();?> </head> |
wp_title() 函数会根据条件标签函数判断当前的页面,生成并输出对应的网页标题。
但是,这种方法在 WordPress 4.4 之后已经不推荐继续使用。
新的标题设置方法
随着 wp_title() 的废弃,与之而来的是新方法。你可以在主题的 functions.php 文件中,添加下边的代码来添加网页标题。
|
/** *新的 WordPress 网页标题设置方法 *https://www.weixiaoduo.com/new-document-title/ */ functionBing_add_theme_support_title(){ add_theme_support('title-tag'); } add_action('after_setup_theme','Bing_add_theme_support_title'); |
代码中,使用 add_theme_support() 函数添加了 「title-tag」 功能,也就是新增的网页标题标签功能。
加入这些代码之后,相当于告诉 WordPress 在 wp_head() 函数处输出一对 <title> 标签,并且包括当前页面的标题。
在这之后,只需保持你的 header.php 中带有 wp_head() 函数即可,不需要再添加 <title> 标签以及 wp_title() 函数。
|
<!DOCTYPE html> <html> <head> <?phpwp_head();?> </head> |
向下兼容
虽然 WordPress 到 4.4 版本才正式放弃 wp_title() 函数,但是新的标题添加方法早在 4.1 就已经添加到了核心代码中。也就是说,新的方法在 4.1 及其以上版本可以运行,但如果要兼容更老的版本,需要同时在 functions.php 中添加下边的代码。
|
/** *兼容 WordPress 4.1 以下版本 *新的 WordPress 网页标题设置方法 *https://www.weixiaoduo.com/new-document-title/ */ functionBing_compat_render_title_tag(){ if(!function_exists('_wp_render_title_tag'))echo'<title>'.wp_title('|',false,'right').'</title>'; } add_action('wp_head','Bing_compat_render_title_tag',1); |
标题过滤器
随着新的方法,同时添加了一些新的过滤器钩子,可以让你控制网页标题的内容。
- pre_get_document_title:可以让你完全控制标题内容,如果该过滤器不返回空,则会将返回值直接当做标题。
- document_title_separator:新的标题分隔符是 「-」,你可以用这个过滤器来修改,比如改成之前的 「|」 。
- document_title_parts:自定义标题的各个组成部分,它们分别是:标题、页码、网站描述和网站名称。
pre_get_document_title
改过滤器可以完全自定义标题的内容,比如把所有页面的标题都改成 「薇晓朵」 。
|
/** *把所有页面的标题都改成 「薇晓朵」 *新的 WordPress 网页标题设置方法 *https://www.weixiaoduo.com/new-document-title/ */ functionBing_pre_get_document_title(){ return'薇晓朵';//自定义标题内容 } add_filter('pre_get_document_title','Bing_pre_get_document_title'); |
这样,每个网页的标题都会变成 「薇晓朵」 。
或者,通过自定义字段的方式来让每篇文章自定义不同的标题。
|
/** *每篇文章自定义不同的标题 *新的 WordPress 网页标题设置方法 *https://www.weixiaoduo.com/new-document-title/ */ functionBing_custom_post_document_title($pre){ if(is_singular()&&$custom=get_post_meta(get_the_ID(),'custom_document_title',true))$pre=$custom; return$pre; } add_filter('pre_get_document_title','Bing_custom_post_document_title'); |
添加这段代码之后,在任何文章中都可以通过 「custom_document_title」 自定义字段来设置网页标题,如果文章没添加自定义字段就会返回空,使用默认标题。
document_title_separator
用于修改标题每个项目之间的分隔符,比如修改成 「|」 。
|
/** *标题分隔符修改成 「|」 *新的 WordPress 网页标题设置方法 *https://www.weixiaoduo.com/new-document-title/ */ functionBing_title_separator_to_line(){ return'|';//自定义标题分隔符 } add_filter('document_title_separator','Bing_title_separator_to_line'); |
之前,大多数人使用的标题分隔符都是 「|」,但现在默认变成了 「-」,会导致网站继续所有页面的标题发生变化,所以可以先改回来。
document_title_parts
这个过滤器会传给我们标题的各个部分,是一个数组,可以修改数组来自定义最终生成的标题。
例如,首页标题默认是 「网站名称 - 网站描述」 的形式,如果你不想要网站描述,可以删除数组中的 tagline 。
|
/** *首页标题不显示网站描述 *新的 WordPress 网页标题设置方法 *https://www.weixiaoduo.com/new-document-title/ */ functionBing_remove_tagline($title){ if(is_home()&&isset($title['tagline']))unset($title['tagline']); return$title; } add_filter('document_title_parts','Bing_remove_tagline'); |