Wordpress - Detecting the Categories of a Post
I was creating a Wordpress child theme (the best way to modify/override your themes) tonight and had the need to have different display options depending on which categories the blog article had. So for example if I had a post categorised with “portfolio”, I wanted to hide the comments form which usually sits at the bottom of a post.
The following snippet will loop over all post categories, if it finds that “portfolio” is one of the categories assigned to the current post then a local variable (boolean) is set which you can use to hide Wordpress functions anywhere in the current template.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
These changes were at the top of my child theme’s single.php template. Not displaying the comments form was as simple as:
Update 12th May 2011
Thanks to Martin in the comments below for alerting me to the fact that Wordpress already has something for this, has_category($category, $post);
Note I didn’t need the 2nd argument as it defaults to the current post.