234
August 18, 2007Excluding WordPress Categories with CSS
Rather than adding a lot of extra PHP code (not to mention the fact I wasn't sure if it was even possible) to exclude this certain category I decided to use a CSS selector to accomplish the task.
The category that I wanted to exclude was named Feature. So by default, when you use
the_category()within the WordPress Loop, the category(ies) is output in HTML as an anchor tag with a link to the category and a unique title attribute. This output is shown in Figure 1.
- <a href="http://website-address.com/category/feature/" title="View all posts in Feature" rel="category tag">Feature</a>
Using CSS selectors I am able to select HTML elements by various attributes, one of these being the title. So if I knew that each time the Feature category was displayed it was going to have that exact title, I could select it with CSS and give it a display value of none to make it disappear. The CSS used to accomplish this is shown in Figure 2.
- a[title="View all posts in Feature"]
- {
- display:none;
- }
That's it! Now whenever the Feature category is output it is hidden by the CSS. Now of course this isn't excluding it completely, if the style sheets are disabled or the source code viewed then the category will be visible. In most cases though, this is sufficient.
To avoid confusion (and unnecessary work!) if you are displaying a list of categories independent of the posts, such as in the sidebar, then you would use
wp_list_categoriesor
wp_dropdown_categoriesand the functionality to exclude certain categories is already included.
