the_category │ filter-hook │ WP 1.2.0

Allows changing a category or the formatted list of post categories.

The filter is applied after the get_the_category_list() function generates the HTML. WordPress also applies it to category names in some admin elements.

Use the_category_list to change the array of category objects before the HTML is generated.

Usage

add_filter( 'the_category', 'wp_kama_the_category_filter', 10, 3 );

/**
 * Function for `the_category` filter-hook.
 * 
 * @param string $thelist   List of categories for the current post.
 * @param string $separator Separator used between the categories.
 * @param string $parents   How to display the category parents. Accepts 'multiple', 'single', or
 *                          empty.
 *
 * @return string
 */
function wp_kama_the_category_filter( $thelist, $separator, $parents ){
	// filter...
	return $thelist;
}
$thelist(string)
Category or list of categories for the current post. Usually contains ready-made HTML markup.
It may be an empty string if the post type does not support categories, or the text Uncategorized if no categories are found.
$separator(string)
Separator between categories.
An empty string means that the list is generated as <ul class="post-categories">.
$parents(string)
How to display parent categories.
Allowed values: multiple, single, or an empty string.

Examples

#1 Replace a category name in the formatted list

add_filter( 'the_category', 'wp_kama_replace_category_name' );

function wp_kama_replace_category_name( $categories ) {
	return str_replace( 'Tools', 'All tools', $categories );
}

Changelog

Since 1.2.0 Introduced.

Where the hook is called

get_the_category_list()
the_category
Walker_Category_Checklist::start_el()
the_category
wp_popular_terms_checklist()
the_category
wp_link_category_checklist()
the_category
wp-includes/category-template.php 230
return apply_filters( 'the_category', $thelist, $separator, $parents );
wp-includes/category-template.php 146
return apply_filters( 'the_category', '', $separator, $parents );
wp-includes/category-template.php 162
return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
wp-admin/edit-tags.php 634
'<strong>' . apply_filters( 'the_category', get_cat_name( get_option( 'default_category' ) ), '', '' ) . '</strong>'
wp-admin/includes/class-walker-category-checklist.php 108
esc_html( apply_filters( 'the_category', $category->name, '', '' ) ) . '</div>';
wp-admin/includes/class-walker-category-checklist.php 120
esc_html( apply_filters( 'the_category', $category->name, '', '' ) ) . '</label>';
wp-admin/includes/template.php 250
echo esc_html( apply_filters( 'the_category', $term->name, '', '' ) );
wp-admin/includes/template.php 298
$name    = esc_html( apply_filters( 'the_category', $category->name, '', '' ) );

Where the hook is used in WordPress

Usage not found.