欢迎您光临爱尚资源网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!
  • 正文概述
  • 更新记录
  • 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=”<?phpthe_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>

    注:原文作者墨鱼

    1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
    2. 分享目的仅供大家学习和交流,请不要用于商业用途!
    3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
    4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
    5. 如有链接无法下载、失效或广告,请联系管理员处理!
    6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
    7. 如遇到加密压缩包,默认解压密码为"dtmb.taobao.com",如遇到无法解压的请联系管理员!
    8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
    声明如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性

    尚艺源码网 » WordPress如何使用the_title()与the_title_attribute()函数

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。 若排除这种情况,可在对应资源底部留言,或 联络我们.。
    你们有qq群吗怎么加入?
    当然有的,如果你是帝国cms、易优cms、和pbootcms系统的爱好者你可以加入我们的QQ千人交流群160457583

    发表评论

    开通VIP 享更多特权,建议使用 QQ 登录