single_cat_titlefilter-hookWP 2.0.10

Allows you to change the title of a category archive page.

The filter fires in single_term_title() and in its derivative function single_cat_title().

It fires only on category pages, when the is_category() conditional tag is true.

Usage

add_filter( 'single_cat_title', 'wp_kama_single_cat_title_filter' );

/**
 * Function for `single_cat_title` filter-hook.
 * 
 * @param string $term_name Category name for archive being displayed.
 *
 * @return string
 */
function wp_kama_single_cat_title_filter( $term_name ){
	// filter...
	return $term_name;
}
$term_name(string)
Category name to use in the category page title.

Examples

#1 Control title output through theme options

This filter can be used to configure title appearance in the theme settings, for example, the title color and the color of its border.

The code excerpt below comes from the ColorMag theme, although nearly identical code is also used by the ColorNews, The NewsMag, Creative Blog, and Madar themes in the repository. Only part of the code is shown to demonstrate the principle.

/**
 * Use hooks to apply category colors to archive titles.
 */
function colormag_colored_category_title( $title ) {

	$color_value        = colormag_category_color( get_cat_id( $title ) );
	$color_border_value = colormag_category_color( get_cat_id( $title ) );

	if ( ! empty( $color_value ) ) {
		return '<h1 class="page-title" style="border-bottom-color: ' . $color_border_value . '">' . '<span style="background-color: ' . $color_value . '">' . $title . '</span></h1>';
	}
	else {
		return '<h1 class="page-title"><span>' . $title . '</span></h1>';
	}
}

function colormag_category_title_function( $category_title ) {
	add_filter( 'single_cat_title', 'colormag_colored_category_title' );
}

#2 Shorten a long title

Suppose the site is not designed for very long titles, and they must be shortened to 20 characters with an ellipsis appended:

add_filter( 'single_cat_title', 'strim_cat_title' );
function strim_cat_title( $title ) {
	return mb_strimwidth( $title, 0, 23, '...' );
}

The drawback is that the text in the <title> tag is also shortened. The solution is to attach the filter after the title is output:

add_action( 'wp_head', function() {
	add_filter( 'single_cat_title', 'trim_cat_title' );
} );

function trim_cat_title( $title ) {
	return mb_strimwidth( $title, 0, 23, '...' );
}

Changelog

Since 2.0.10 Introduced.

Where the hook is called

single_term_title()
single_cat_title
wp-includes/general-template.php 1825
$term_name = apply_filters( 'single_cat_title', $term->name );

Where the hook is used in WordPress

wp-includes/default-filters.php 177
add_filter( $filter, 'wptexturize' );
wp-includes/default-filters.php 178
add_filter( $filter, 'strip_tags' );