关于Wordpress get_the_title函数的混淆

时间:2010-09-02 18:29:16

标签: 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 );
}

我不明白参数__('Protected: %s')在下面的特定代码行中的含义。它是什么样的参数?

$protected_title_format = apply_filters('protected_title_format', __('Protected: %s'));

1 个答案:

答案 0 :(得分:1)

__()是一个本地化函数,用于获取英文单词“Protected”的本地化字符串。

%ssprintf()使用的替换参数。基本上,它取代了博客文章的标题。

整个__('Protected: %s')调用作为参数传递给apply_filters()函数,以简单地格式化帖子标题。默认情况下,我认为没有任何事情发生,但插件可以挂钩protected_title_format过滤器,以便在应用帖子标题之前进一步操作格式。