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 均用于在文章页面展示标题。将二者合并讨论是因为它们底层实现一致,区别仅在于 the_title 默认直接输出,而 get_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-includes/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-includes/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 );
}
?>