WordPress如何使用the_title()与the_title_attribute()函数
wordpress 提供了一个非常简单方便的函数来显示当前文章的标题,那就是:the_title()。
这个函数经常被开发者在 header,post,page,loop,footer 里使用,这几乎是开发主题里最常用的wordpress函数之一,然而许多开发者并没有意识到这里有个地方并不应该使用此函数,那就是在 attributes 里,如:
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">继续阅读 <?php the_title(); ?></a>
很多开发者在 loop,page,post 里使用这样的写法设置一个超链接到指定的文章,看起来似乎并没有什么问题,但其实正确安全的写法应该把 title=”<?php the_title(); ?>” 改写成 title=”<?php the_title_attribute(); ?>”
为什么要这样写,大家看看 wordpress 源文件中的相关函数核心文件便知了:
the_title() 源代码:
/** * 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() 函数,我们再看下这个函数的相关文件.
/** * 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 mixed $post Optional. Post ID or object. * @return string */function get_the_title( $post = 0 ) { $post = get_post( $post ); $title = isset( $post->post_title ) ? $post->post_title : ''; $id = isset( $post->ID ) ? $post->ID : 0; 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 );}
这个函数非常简单,它用 get_post() 取回了post object,然后把它传递给一个叫做 the_title的filter,返回 $post->post_title
这个函数最重要的地方就是 apply_filters( ‘the_title’, $title, $id );
这个 filter 可以提供给开发者自定义标题的输出形式,比如添加额外的 html 标签。
the_title_attribute() 源代码:
/** * Sanitize the current title when retrieving or displaying. * * Works like {@link the_title()}, except the parameters can be in a string or * an array. See the function for what can be override in the $args parameter. * * The title before it is displayed will have the tags stripped and {@link * esc_attr()} before it is passed to the user or displayed. The default * as with {@link the_title()}, is to display the title. * * @since 2.3.0 * * @param string|array $args Optional. Override the defaults. * @return string|null Null on failure or display. String when echo is false. */function the_title_attribute( $args = '' ) { $title = get_the_title(); if ( strlen($title) == 0 ) return; $defaults = array('before' => '', 'after' => '', 'echo' => true); $r = wp_parse_args($args, $defaults); extract( $r, EXTR_SKIP ); $title = $before . $title . $after; $title = esc_attr(strip_tags($title)); if ( $echo ) echo $title; else return $title;}
这个函数也使用了 get_the_title() 函数来取回文章的标题,但是最后返回的数据却与the_title() 函数不同。这里过滤掉了许多转义字符与html标签,能够更加安全的在元素属性里进行使用。
详细例子:
假设你的 $post->post_title 是这样的
<span class="title">这是有span标签的标题</span>
当你使用 the_title() 函数,输出将保持不变,还是如下
<span class="title">这是有span标签的标题</span>
但是当你使用 the_title_attribute(),你的输出是如下的
这是有span标签的标题
注意这里的span标签已经被移除掉了.
又假如如果你的标题里有双引号,如下
这是一个带 "双引号" 的标题
当你使用 the_title() 函数,输出如下
这是一个带 "双引号" 的标题
但是当你使用 the_title_attrubute() 函数,输出却如下
这是一个带 /"双引号/" 的标题
注意到这里自动把双引号替换成转义字符了,这样就保证了html标签属性的安全使用。
如果我们在html标签属性里使用 the_title() 函数,则会破坏掉属性原有的形式
<span title="<?php the_title(); ?>"><?php the_title(); ?></span>
输出将会如下:
<span title="这是一个带 "双引号" 的标题">这是一个带”双引号”的标题</span>
注意到了这里的title属性的引号,html标签对引号的使用是非常严格的,禁止这样的形式出现,一旦出现将导致页面严重的显示问题.
经过以上的分析,希望开发者们在以后的开发过程中能注意到这些小细节,在html标签属性里一定要使用 the_title_attribute() 函数而不是 the_title() 函数!
所以正确的用法应该是:
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">继续阅读 <?php the_title(); ?></a>
注:原文作者墨鱼
-
无相关信息
- 宝塔面板FTP链接服务器发回了不可路由地址
- 阿里云ecs安装宝塔面板失败原因?以及安装步骤?
- 织梦手机端-识别跳转
- html如何在一个form表单中实现多个submit
- thinkphp无限分类原理和实现代码
- thinkphp6 请求(request)变量
- 帝国CMS自定义列表SQL调用方法
- 帝国CMS自定义列表的使用实例
- 帝国cms自定义列表限制某个栏目显示
- PHP中利用for循环判断一个数是不是回文数
- PHP中判断一个数是否为素数的方法
- PHP中break,continue,exit的使用与区别
- 测试PHP连接MYSQL成功与否的代码
- PHP编程求最大公约数与最小公倍数的方法示例
- PHP获取数组中单列值的方法
- PHP使用一个for循环将N*N的二维数组的所有值置1实现方法
- HTML5+css3:3D旋转木马效果相册
- HTML,CSS,font-family:中文字体和英文名称对照表
- HTML使用栅格布局实现六种筛子样式的代码详解
- 修改CSS样式实现网页变灰色/黑白代码的几个方法整理
- 黄庭坚《水调歌头·游览》我欲穿花寻路 直入白云深处
- 黄庭坚《品令·茶词》恰如灯下 故人万里 归来对影
- 杜荀鹤《溪兴》山雨溪风卷钓丝 瓦瓯篷底独斟时
- 储光羲《张谷田舍》一径入寒竹 小桥穿野花
- 赵嘏《寒塘》晓发梳临水 寒塘坐见秋
- 蔡确《夏日登车盖亭》纸屏石枕竹方床 手倦抛书午梦长
- 李峤《书》河图八卦出 洛范九畴初
- 刘长卿《饯别王十一南游》长江一帆远 落日五湖春
- 韦庄《章台夜思》芳草已云暮 故人殊未来
- 卢纶《宿澄上人院》竹窗闻远水 月出似溪中
- 孟浩然《留别王侍御维》只应守寂寞 还掩故园扉
- 苏轼《南乡子·送述古》归路晚风清 一枕初寒梦不成
- 王维《木兰柴》秋山敛馀照 飞鸟逐前侣
- 王维《栾家濑》跳波自相溅 白鹭惊复下
- 苏轼《少年游·润州作代人寄远》对酒卷帘邀明月 风露透窗纱
- 苏轼《行香子·过七里濑》一叶舟轻 双桨鸿惊 水天清 影湛波平
- 王维《汉江临泛》江流天地外 山色有无中
- 柳宗元《溪居》晓耕翻露草 夜榜响溪石
- 杜牧《赠宣州元处士》蓬蒿三亩居 宽于一天下
- 项鸿祚《清平乐·池上纳凉》水天清话 院静人销夏