This is an older version of Design Intellection. Access the new one: http://designintellection.com/.

Design Intellection is a small web design company committed to making the web a better place.

Contact

Subscribe to the blog feed

234

Excluding 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.

  1. <a href="http://website-address.com/category/feature/" title="View all posts in Feature" rel="category tag">Feature</a>
Figure 1: The HTML that WordPress outputs for the Feature category.
(Note: This example uses .htaccess configured permalinks.)

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.

  1. a[title="View all posts in Feature"]
  2. {
  3. display:none;
  4. }
Figure 2: The CSS used to make the Feature category disappear.

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_categories

or

wp_dropdown_categories

and the functionality to exclude certain categories is already included.

Send a Message Have something to say? Use the form below to email your comment directly to me.
If warranted, I’ll do my best to respond in a timely matter.

Legend * = Required

(Hint: It’s David)

Content © David Yeiser, 2007–2009 | Published with Habari.