函数文档

single_cat_title()

💡 云策文档标注

概述

single_cat_title() 函数用于在分类归档页面显示或检索分类标题,适用于分类模板文件。它通过调用 single_term_title() 实现,前缀参数不会自动添加空格,需手动处理。

关键要点

  • 功能:显示或检索分类归档页面的标题,常用于分类模板。
  • 参数:$prefix(可选字符串,标题前缀,需自行处理空格);$display(可选布尔值,控制显示或检索,默认 true 为显示)。
  • 返回值:检索时返回字符串标题,显示时无返回值。
  • 实现:内部调用 single_term_title() 函数。
  • 相关函数:与 single_term_title() 和 get_the_archive_title() 关联。
  • 版本历史:自 WordPress 0.71 版本引入。

代码示例

// 示例1:显示“Currently browsing ”后跟分类标题
single_cat_title('Currently browsing ');

// 示例2:检索当前分类标题到变量
$current_category = single_cat_title('', false);

// 示例3:在 functions.php 中移除分类页面标题中的“Category:”前缀
function wporg_remove_category_title($title) {
    if (is_category()) {
        $title = single_cat_title('', false);
    }
    return $title;
}
add_filter('remove_the_archive_title', 'wporg_remove_category_title');

注意事项

  • 前缀参数 $prefix 不会自动添加空格,如需空格,应在参数值末尾手动添加。
  • 示例代码中,移除“Category:”前缀的过滤器可能仅适用于顶级分类,不适用于子分类,使用时需注意。
  • 用户贡献笔记指出,示例代码缺少翻译支持,建议确保字符串可翻译以符合国际化最佳实践。

📄 原文内容

Displays or retrieves page title for category archive.

Description

Useful for category template files for displaying the category page title.
The prefix does not automatically place a space between the prefix, so if there should be a space, the parameter value will need to have it at the end.

Parameters

$prefixstringoptional
What to display before the title.
$displaybooloptional
Whether to display or retrieve title.

Default:true

Return

string|void Title when retrieving.

Source

function single_cat_title( $prefix = '', $display = true ) {
	return single_term_title( $prefix, $display );
}

Changelog

Version Description
0.71 Introduced.

User Contributed Notes

  1. Skip to note 6 content

    Example: If you want to remove “Category:” in title from category pages just copy and paste in function.php.

    function wporg_remove_category_title( $title ) {
        if ( is_category() ) {
            $title = single_cat_title( '', false );
        }
        return $title;
    }
    add_filter( 'remove_the_archive_title', 'wporg_remove_category_title' );