WordPress开发中用于标题显示的相关函数使用解析


Posted in PHP onJanuary 07, 2016

single_cat_title()函数
single_cat_title()函数,日常中我们很少会用到,但这个函数会给我们解决很多问题,诸如当前页面的目录、标签,该函数不依附于 WordPress 主循环中,也不能放入主循环中使用。

描述
获取当前页面的分类、标签。

<?php single_cat_title($prefix,$display); ?>
  • $prefix :用于设置在标题之前显示的内容。
  • $display :用于设置是直接显示还是返回到变量。

实例
在此摘取 WordPress 2011 默认主题中,category.php 文件 第18行左右位置的代码

<?php
printf( __( 'Category Archives: %s', 'twentyeleven' ), '<span>' . single_cat_title( '', false ) . '</span>' );
?>

get_the_title 和 the_title
get_the_title 和 the_title 两个函数用来在文章页面显示文章标题的函数,之所以将两个函数合并到一篇文章里面去是因为这两个函是一个实现,只不过 the_title 默认直接显示,get_the_title 默认返回字符串,如果你对此心存疑惑,那请你往下看。

函数详解
get_the_title 和 the_title这两个函数主要用于在循环中显示当前文章的标题,请注意 the_title 这个函数必须使用在循环中。
两者的区别在于,get_the_title仅能以字符串形式返回文章标题,而 the_title 可以设置标题前后的自定义字符,以及是显示还是返回字符串。

the_title 函数使用、参数详解

<?php the_title( $before, $after, $echo ); ?>
  • $before标题前的字符
  • $after标题后的字符
  • $echo显示、还是返回字符串,默认为true

the_title示例

<?php the_title( ‘=>', ‘<=' ); ?>

以本文为例,我们将得到以下这样的标题:

‘=>get_the_title 和 the_title<='

get_the_title 函数使用、参数详解

<?php $myTitle = get_the_title($ID); ?>

以上代码我们将得到文章标题的变量$myTitle;
$ID 用于设置文章 ID ,当然在循环中我们可以省略此参数。

get_the_title 示例

<?php
 $myTitle = get_the_title($ID); 
 echo $mytitle.'【标题演示】';
?>

我们将得到

get_the_title 和 the_title【标题演示】

总结
说了这么多,不知道对您是否有所帮助?
总的来说 the_title 是 get_the_title的更高一级封装。就像在 wp_title中说的那样,更高级封装,虽然使用起来简单,但能折腾花样相对少了点。
下面是该两个函数的源代码

the_title 函数声明
该函数位于 wp-include/post-template.php 文件的 43 ? 55行左右的位置

<?php
/**
 * Display or retrieve the current post title with optional content.
 *
 * @since 0.71
 *
 * @param string $before Optional. Content to prepend to the title.
 * @param string $after Optional. Content to append to the title.
 * @param bool $echo Optional, default to true.Whether to display or return.
 * @return null|string Null on no title. String if $echo parameter is false.
 */
function the_title($before = '', $after = '', $echo = true) {
 $title = get_the_title();
 
 if ( strlen($title) == 0 )
 return;
 
 $title = $before . $title . $after;
 
 if ( $echo )
 echo $title;
 else
 return $title;
}
?>

get_the_title 函数声明
该函数位于 wp-include/post-template.php 文件的 103 ? 118行左右的位置

<?php
/**
 * Retrieve post title.
 *
 * If the post is protected and the visitor is not an admin, then "Protected"
 * will be displayed before the post title. If the post is private, then
 * "Private" will be located before the post title.
 *
 * @since 0.71
 *
 * @param int $id Optional. Post ID.
 * @return string
 */
function get_the_title( $id = 0 ) {
 $post = &get_post($id);
 
 $title = isset($post->post_title) ? $post->post_title : '';
 $id = isset($post->ID) ? $post->ID : (int) $id;
 
 if ( !is_admin() ) {
 if ( !empty($post->post_password) ) {
  $protected_title_format = apply_filters('protected_title_format', __('Protected: %s'));
  $title = sprintf($protected_title_format, $title);
 } else if ( isset($post->post_status) && 'private' == $post->post_status ) {
  $private_title_format = apply_filters('private_title_format', __('Private: %s'));
  $title = sprintf($private_title_format, $title);
 }
 }
 return apply_filters( 'the_title', $title, $id );
}
?>
PHP 相关文章推荐
拼音码表的生成
Oct 09 PHP
PHP数组的交集array_intersect(),array_intersect_assoc(),array_inter_key()函数的小问题
May 29 PHP
php数据结构与算法(PHP描述) 查找与二分法查找
Jun 21 PHP
linux环境apache多端口配置虚拟主机的方法深入介绍
Jun 09 PHP
一个PHP的远程图片抓取函数分享
Sep 25 PHP
php教程之魔术方法的使用示例(php魔术函数)
Feb 12 PHP
ThinkPHP标签制作教程
Jul 10 PHP
CI框架入门示例之数据库取数据完整实现方法
Nov 05 PHP
PHP判断IP并转跳到相应城市分站的方法
Mar 25 PHP
CI框架出现mysql数据库连接资源无法释放的解决方法
May 17 PHP
XHProf报告字段含义的解析
May 17 PHP
详解php中流行的rpc框架
May 29 PHP
PHP中strcmp()和strcasecmp()函数字符串比较用法分析
Jan 07 #PHP
WordPress中调试缩略图的相关PHP函数使用解析
Jan 07 #PHP
PHP中substr函数字符串截取用法分析
Jan 07 #PHP
PHP中addcslashes与stripcslashes函数用法分析
Jan 07 #PHP
使用php+swoole对client数据实时更新(一)
Jan 07 #PHP
PHP中addslashes()和stripslashes()实现字符串转义和还原用法实例
Jan 07 #PHP
PHP中ltrim与rtrim去除左右空格及特殊字符实例
Jan 07 #PHP
You might like
php抓即时股票信息
2006/10/09 PHP
学习php设计模式 php实现策略模式(strategy)
2015/12/07 PHP
PHP下载远程图片的几种方法总结
2017/04/07 PHP
php实现数组纵向转横向并过滤重复值的方法分析
2017/05/29 PHP
thinkPHP框架实现多表查询的方法
2018/06/14 PHP
JavaScript 的方法重载效果
2009/08/07 Javascript
Jquery常用技巧收集整理篇
2010/11/14 Javascript
Jquery 绑定时间实现代码
2011/05/03 Javascript
js预载入和JavaScript Image()对象使用介绍
2011/08/28 Javascript
js简单实现删除记录时的提示效果
2013/12/05 Javascript
从数组中随机取x条不重复数据的JS代码
2013/12/24 Javascript
JavaScript动态加载样式表的方法
2015/03/21 Javascript
jQuery菜单插件superfish使用指南
2015/04/21 Javascript
jQuery页面弹出框实现文件上传
2017/02/09 Javascript
jquery ajax加载数据前台渲染方式 不用for遍历的方法
2018/08/09 jQuery
微信小程序下拉刷新PullDownRefresh的使用方法
2018/11/29 Javascript
Vue指令之 v-cloak、v-text、v-html实例详解
2019/08/08 Javascript
JavaScript 斐波那契数列 倒序输出 输出100以内的质数代码实例
2019/09/11 Javascript
[44:50]2018DOTA2亚洲邀请赛 4.1 小组赛 A组 TNC vs VG
2018/04/02 DOTA
python 布尔操作实现代码
2013/03/23 Python
使用python实现knn算法
2017/12/20 Python
python3使用smtplib实现发送邮件功能
2018/05/22 Python
python 实现语音聊天机器人的示例代码
2018/12/02 Python
Django 迁移、操作数据库的方法
2019/08/02 Python
基于Django框架的权限组件rbac实例讲解
2019/08/31 Python
Python sklearn库实现PCA教程(以鸢尾花分类为例)
2020/02/24 Python
Selenium alert 弹窗处理的示例代码
2020/08/06 Python
CSS书写规范、顺序和命名规则
2014/03/06 HTML / CSS
乔丹诺(Giordano)酒庄德国官网:找到最好的意大利葡萄酒
2017/12/28 全球购物
俄罗斯运动鞋商店:Sneakerhead
2018/05/10 全球购物
生日寿宴答谢词
2014/01/19 职场文书
电焊工岗位工作职责
2014/07/09 职场文书
群众路线党员个人剖析材料
2014/10/08 职场文书
2015大学生党员自我评价范文
2015/03/03 职场文书
歌舞青春观后感
2015/06/10 职场文书
2015年教师节广播稿
2015/08/19 职场文书