The post_class_taxonomies WordPress PHP filter allows you to modify the taxonomies used to generate classes for each individual term in a post.
Usage
add_filter('post_class_taxonomies', 'your_custom_function', 10, 4);
function your_custom_function($taxonomies, $post_id, $classes, $css_class) {
// your custom code here
return $taxonomies;
}
Parameters
- $taxonomies (string[]): List of all taxonomy names to generate classes for.
- $post_id (int): The post ID.
- $classes (string[]): An array of post class names.
- $css_class (string[]): An array of additional class names added to the post.
More information
See WordPress Developer Resources: post_class_taxonomies
Examples
Add a custom taxonomy to post classes
This example adds a custom taxonomy named “color” to the post classes.
add_filter('post_class_taxonomies', 'add_color_taxonomy', 10, 4);
function add_color_taxonomy($taxonomies, $post_id, $classes, $css_class) {
$taxonomies[] = 'color';
return $taxonomies;
}
Remove category taxonomy from post classes
This example removes the category taxonomy from post classes.
add_filter('post_class_taxonomies', 'remove_category_taxonomy', 10, 4);
function remove_category_taxonomy($taxonomies, $post_id, $classes, $css_class) {
if(($key = array_search('category', $taxonomies)) !== false) {
unset($taxonomies[$key]);
}
return $taxonomies;
}
Remove all taxonomies except tags
This example keeps only the “post_tag” taxonomy for generating post classes.
add_filter('post_class_taxonomies', 'keep_only_tags', 10, 4);
function keep_only_tags($taxonomies, $post_id, $classes, $css_class) {
return array('post_tag');
}
Add prefix to taxonomy names
This example adds a “tx-” prefix to all taxonomy names in the post classes.
add_filter('post_class_taxonomies', 'prefix_taxonomies', 10, 4);
function prefix_taxonomies($taxonomies, $post_id, $classes, $css_class) {
return array_map(function($taxonomy) {
return 'tx-' . $taxonomy;
}, $taxonomies);
}
Include only specific taxonomies
This example generates post classes only for “category” and “post_tag” taxonomies.
add_filter('post_class_taxonomies', 'specific_taxonomies', 10, 4);
function specific_taxonomies($taxonomies, $post_id, $classes, $css_class) {
return array('category', 'post_tag');
}