WordPress exclude categories from the_category

This is an update to an excellent old post on wordpress.com that partially explains how to exclude categories from the_category() function in WordPress.

ocshawn’s solution half way down the page worked with a few small adjustments. Add the following to your WP functions.php (ideally in a child theme):

function exclude_post_categories($excl=''){
$categories = get_the_category($post->ID);
$exclude = explode(",", $exclude);
$html = '';
foreach ($categories as $cat) {
if(!in_array($cat->cat_ID, $exclude)) {
$html .= ' $html .= 'title="' . $cat->cat_name . '">' . $cat->cat_name . ', ';
echo substr($html, 0, strrpos($html, ', '));

then when you need to call the function use


where ‘1,2,17’ are the category codes you want to omit.

Note: one crucial thing not mentioned here is that permalinks must be of the default form ( ?p=123 ) so that category numbers are identified by the code.