php the title,WordPress教程:如何使用the_title()与the_title_attribute()函数

2958aa523b8e4e9e9dc07d7fa092534b.png

WordPress 提供了一个非常简单方便的函数来显示当前文章的标题,那就是:the_title()。

106da387dabce3fc04848b686f087509.png

这个函数经常被开发者在 header,post,page,loop,footer 里使用,这几乎是开发主题里最常用的Wordpress函数之一,然而许多开发者并没有意识到这里有个地方并不应该使用此函数,那就是在 attributes 里,如:

继续阅读 <?php the_title(); ?>

很多开发者在 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标签的标题

当你使用 the_title() 函数,输出将保持不变,还是如下

这是有span标签的标题

但是当你使用 the_title_attribute(),你的输出是如下的

这是有span标签的标题

注意这里的span标签已经被移除掉了.

又假如如果你的标题里有双引号,如下

这是一个带 "双引号" 的标题

当你使用 the_title() 函数,输出如下

这是一个带 "双引号" 的标题

但是当你使用 the_title_attrubute() 函数,输出却如下

这是一个带 \"双引号\" 的标题

注意到这里自动把双引号替换成转义字符了,这样就保证了html标签属性的安全使用。

如果我们在html标签属性里使用 the_title() 函数,则会破坏掉属性原有的形式

输出将会如下:

这是一个带”双引号”的标题

注意到了这里的title属性的引号,html标签对引号的使用是非常严格的,禁止这样的形式出现,一旦出现将导致页面严重的显示问题.

经过以上的分析,希望开发者们在以后的开发过程中能注意到这些小细节,在html标签属性里一定要使用 the_title_attribute() 函数而不是 the_title() 函数!

所以正确的用法应该是:

继续阅读 <?php the_title(); ?>

注:原文作者墨鱼

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值