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 相关文章推荐
php URL验证正则表达式
Jul 19 PHP
php ajax 静态分页过程形式
Sep 02 PHP
PHP延迟静态绑定示例分享
Jun 22 PHP
PHP依赖倒置(Dependency Injection)代码实例
Oct 11 PHP
浅谈thinkphp的实例化模型
Jan 04 PHP
php实现用手机关闭计算机(电脑)的方法
Apr 22 PHP
php实现json编码的方法
Jul 30 PHP
yii去掉必填项中星号的方法
Dec 28 PHP
Yii2 rbac权限控制之rule教程详解
Jun 23 PHP
PHP命名空间namespace用法实例分析
Sep 27 PHP
利用PHP生成CSV文件简单示例
Dec 21 PHP
基于PHP实现邮箱验证激活过程详解
Oct 28 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 5.3.5安装memcache注意事项小结
2011/04/12 PHP
php+ajax实现图片文件上传功能实例
2014/06/17 PHP
PHP用mysql_insert_id()函数获得刚插入数据或当前发布文章的ID
2016/11/25 PHP
thinkPHP3.2.3结合Laypage实现的分页功能示例
2018/05/28 PHP
javascript十个最常用的自定义函数(中文版)
2009/09/07 Javascript
jquery中的 $(&quot;#jb51&quot;)与document.getElementById(&quot;jb51&quot;) 的区别
2011/07/26 Javascript
关于JS管理作用域的问题
2013/04/10 Javascript
js,jquery滚动/跳转页面到指定位置的实现思路
2014/06/03 Javascript
jQuery+slidereveal实现的面板滑动侧边展出效果
2015/03/14 Javascript
jquery弹出遮掩层效果【附实例代码】
2016/04/28 Javascript
利用vscode编写vue的简单配置详解
2017/06/17 Javascript
JavaScript简单实现合并两个Json对象的方法示例
2017/10/16 Javascript
在GitHub Pages上使用Pelican搭建博客的教程
2015/04/25 Python
Python中random模块用法实例分析
2015/05/19 Python
Python正则表达式教程之三:贪婪/非贪婪特性
2017/03/02 Python
python 爬虫 批量获取代理ip的实例代码
2018/05/22 Python
解决pycharm无法识别本地site-packages的问题
2018/10/13 Python
Django异步任务之Celery的基本使用
2019/03/23 Python
python添加菜单图文讲解
2019/06/04 Python
Django Form设置文本框为readonly操作
2020/07/03 Python
详解python的super()的作用和原理
2020/10/29 Python
猫途鹰:全球领先的旅游点评社区
2017/04/07 全球购物
英国设计师珠宝网站:Joshua James Jewellery
2020/03/01 全球购物
俄语专业毕业生推荐信
2013/10/28 职场文书
高分子材料与工程专业个人求职信
2013/12/15 职场文书
中英文自我评价常用句型
2013/12/19 职场文书
平安建设工作方案
2014/06/02 职场文书
社区党建工作汇报材料
2014/08/14 职场文书
2014第二批党的群众路线教育实践活动对照检查材料思想汇报
2014/09/18 职场文书
采购员工作总结范文
2015/08/12 职场文书
校运会班级霸气口号
2015/12/24 职场文书
如何拟写通知正文?
2019/04/02 职场文书
干货分享:推荐信写作技巧!
2019/06/21 职场文书
Python 发送SMTP邮件的简单教程
2021/06/24 Python
简单聊聊TypeScript只读修饰符
2022/04/06 Javascript
基于Python编写一个监控CPU的应用系统
2022/06/25 Python