详解WordPress开发中wp_title()函数的用法


Posted in PHP onJanuary 07, 2016

wp_title 函数在 WordPress 中是用来显示文章、页面、分类等等等等标题的一个函数,但在首页索引,该函数将不显示任何的东西。该函数在 WordPress 官方主题中一直被使用,但目前很多定制的主题中这个函数总是为忽视。

函数意义详解
wp_title 函数用来显示页面的标题,如在文章页面,则显示文章标题;在分类页面,则显示分类名称,但在首页索引,该函数将不显示任何的东西。
有点像 WordPress 中的 get_the_title 和 single_cat_title()这两个函数的自适应用法(自动判断是页面、文章还是分类、归档、标签)。

函数声明
有点长,希望您能耐心看一遍,哪怕只有那么一遍。

/**
 * 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 $wpdb, $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 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();
 $tax = get_taxonomy( $term->taxonomy );
 $title = single_term_title( $tax->labels->name . $t_sep, false );
}
 
// If there's an author
if ( is_author() ) {
 $author = get_queried_object();
 $title = $author->display_name;
}
 
// If there's a post type archive
if ( is_post_type_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 ";
 
// Determines position of the separator and direction of the breadcrumb
if ( 'right' == $seplocation ) { // sep on right, so reverse the order
$title_array = explode( $t_sep, $title );
$title_array = array_reverse( $title_array );
$title = implode( " $sep ", $title_array ) . $prefix;
} else {
 $title_array = explode( $t_sep, $title );
 $title = $prefix . implode( " $sep ", $title_array );
}
 
$title = apply_filters('wp_title', $title, $sep, $seplocation);
 
// Send it out
 if ( $display )
 echo $title;
 else
 return $title;
 
}

用法

<?php wp_title( $sep, $echo, $seplocation ); ?>

参数详解

  • $sep:分隔符;
  • $echo:是否显示;
  • $seplocation:分隔符所在位置(左还是右,只接受'right',如果不是right自动判定为左)

总结

WordPress 中相同功能的函数有很多,都是从基层到高级不断的经过封装最后到达使用层的,当然如果我们需要一些灵活用法的话,我们可以直接用中间那层的函数,如果我们懒的话我们可以直接使用最高级的那层函数,诸如本函数 wp_title ,其实这个函数我们从源代码来看, wp 替我们针对 分类、标签、文章、归档、作者、页面等多种类型的页面进行了判断,并根据不同页面调用不同的标题函数来达到目的。
如果有时间,您可以对下面几个函数进行深入研究一下,一遍更灵活的进行seo
single_post_title 文章页面提取标题的函数
single_term_title tag(标签)、cat(分类)、日期、提取标题的函数 类似于 single_cat_title()函数
get_queried_object 作者页面提取对象的函数(对象中有作者名)
post_type_archive_title()规档等等提取标题的函数
还等什么?
GO GO GO !

PHP 相关文章推荐
使用adodb lite解决问题
Dec 31 PHP
PHP 地址栏信息的获取代码
Jan 07 PHP
DW中链接mysql数据库时,建立字符集中文出现乱码的解决方法
Mar 27 PHP
php制作unicode解码工具(unicode编码转换器)代码分享
Dec 24 PHP
php连接odbc数据源并保存与查询数据的方法
Dec 24 PHP
深入探究PHP的多进程编程方法
Aug 18 PHP
PHP中的use关键字及文件的加载详解
Nov 28 PHP
Yii2中简单的场景使用介绍
Jun 02 PHP
php mysql PDO 查询操作的实例详解
Sep 23 PHP
php实现二叉树中和为某一值的路径方法
Oct 14 PHP
php workerman定时任务的实现代码
Dec 23 PHP
PHP使用QR Code生成二维码实例
Jul 07 PHP
PHP中strncmp()函数比较两个字符串前2个字符是否相等的方法
Jan 07 #PHP
PHP编程基本语法快速入门手册
Jan 07 #PHP
PHP中strnatcmp()函数“自然排序算法”进行字符串比较用法分析(对比strcmp函数)
Jan 07 #PHP
WordPress开发中用于标题显示的相关函数使用解析
Jan 07 #PHP
PHP中strcmp()和strcasecmp()函数字符串比较用法分析
Jan 07 #PHP
WordPress中调试缩略图的相关PHP函数使用解析
Jan 07 #PHP
PHP中substr函数字符串截取用法分析
Jan 07 #PHP
You might like
php实现递归与无限分类的方法
2015/02/16 PHP
php抽象方法和抽象类实例分析
2016/12/07 PHP
PHP文件上传小程序 适合初学者学习!
2019/05/23 PHP
javascript中巧用“闭包”实现程序的暂停执行功能
2007/04/04 Javascript
js判断两个日期是否相等的方法
2013/09/10 Javascript
JavaScript获得当前网页来源页面(即上一页)的方法
2015/04/03 Javascript
浅谈js多维数组和hash数组定义和使用
2016/07/27 Javascript
nodejs实例解析(输出hello world)
2017/01/03 NodeJs
Vue表单验证插件Vue Validator使用方法详解
2017/04/07 Javascript
Angular父组件调用子组件的方法
2018/04/02 Javascript
JavaScript 高性能数组去重的方法
2018/09/20 Javascript
vue上传图片到oss的方法示例(图片带有删除功能)
2018/09/27 Javascript
在NPM发布自己造的轮子的方法步骤
2019/03/09 Javascript
微信小程序实现吸顶特效
2020/01/08 Javascript
ES6中Promise的使用方法实例总结
2020/02/18 Javascript
微信小程序实现滑动操作代码
2020/04/23 Javascript
js实现微信聊天界面
2020/08/09 Javascript
详解Vue数据驱动原理
2020/11/17 Javascript
[01:11]辉夜杯战队访谈宣传片—CDEC.Y
2015/12/26 DOTA
[04:03][TI9趣味短片] 小鸽子茶话会
2019/08/20 DOTA
自己编程中遇到的Python错误和解决方法汇总整理
2015/06/03 Python
深入理解python多进程编程
2016/06/12 Python
python3解析库BeautifulSoup4的安装配置与基本用法
2018/06/26 Python
使用Python和Prometheus跟踪天气的使用方法
2019/05/06 Python
python爬虫beautifulsoup库使用操作教程全解(python爬虫基础入门)
2021/02/19 Python
建筑工程技术应届生求职信
2013/11/17 职场文书
在校生自我鉴定
2014/01/23 职场文书
学校联谊活动方案
2014/02/15 职场文书
入党自荐书范文
2014/03/09 职场文书
学校三八妇女节活动情况总结
2014/03/09 职场文书
公务员试用期满考核材料
2014/05/22 职场文书
社区服务理念口号
2015/12/25 职场文书
2016年大学生社区服务活动总结
2016/04/06 职场文书
微信小程序用户授权最佳实践指南
2021/05/08 Javascript
Vue实现导入Excel功能步骤详解
2021/07/03 Vue.js
Python如何使用循环结构和分支结构
2022/04/13 Python