Hack: Altering Post Titles Depending On Categories

So here is something I wanted to do for this site. I want to put “Tip:” before post titles that are in the tips category, and “Hack:” before titles that in the hacks category. I don’t want to just prepend the category name, as that was include the “s” and I don’t want it done for anything other than these two categories.

This is a very simple filter you can add to functions.php or your own plugin. It just checks the category for those two choices and returns an altered title if either is found. The first one has precedence, of course. Otherwise, the regular title is returned.

add_filter('the_title','wpfilter_category_before_title',10,2);
function wpfilter_category_before_title($title,$id) {
	if (has_category('Tips',$id)) return "Tip: ".$title;
	if (has_category('Hacks',$id)) return "Hack: ".$title;
	return $title;
}

Leave a Reply

Your email address will not be published. Required fields are marked *