Spruce interactive


Category Class/ID Names in WordPress

Posted: June 1st, 2012

Author: KJ Parish


, , , , ,

 Details

If you create custom themes in WordPress and have often wished for a simple, uncomplicated way of using post category names as a class or ID name, then this next little tip is for you.

Category Sprites Example

Let’s say you’re building a cool food blog with 3 primary (although slightly unrealistic and entertaining) categories:

  • Sushi
  • Lobster
  • Turkey

You’ve just created a nice sprite which includes individual icons related to each category above. You know, something along the lines of:

Great – good job so far.

Now…onto the really fun stuff. You know you’ll be displaying the latest of these awesome food posts on the home page, and each post needs to display the correct icon for the category it belongs to. In addition, wouldn’t it be nice if you could get to the lobster archive by clicking on the lobster? No problem. To display each sprite, we’ll use some basic css, and we’ll ask WordPress to display the category slug as an ID name for each category so we can style these linked icons with ease. Here we go.

The Code You Seek

<?php foreach((get_the_category()) as $category)  {
   echo  '<a href="' . get_category_link( $category->term_id ) . '"id="'. $category->slug .'" class="sprites"  title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . ' ></a>'; }

In short…get the category link, use the category slug as the ID, add the class “sprite,” and give each link the following title while you’re at it: View all posts in [Category Name], which results in:

<a href="http://www.awesomefoodblog.com/lobster" id="lobster" class="sprite"  title="View all posts in Lobster"></a>

To display your icon from your sprite, your css might be something along the lines of:

a#lobster {
  background-position:-293px -46px;

.sprite {
  background:url(images/sprite.png) no-repeat left top;

And there you have it.

Talk to Us

Wise men have said, "All good things must come to an end", and so it was with Spruce. We began our first project in 2006, and completed our last in Fall 2013. It's been a wonderful experience, but now it's time to begin the next adventure.

Though...we wouldn't want to leave you hanging, now would we? Here are our top recommendations for world-class branding, design, and web development:

  • Kristina (KJ) Parish and Beam Collective:
    KJ provided the artistic vision that allowed Spruce to succeed, and we still think she's the greatest designer in the entire world. She's started a new collective with some talented developers, and is covering much the same territory as Spruce--design and development all under one roof, with a caliber of service and professionalism seldom seen in this business or any other. If you like what you see on the Spruce site, then definitely get in touch with KJ at kj@beamcollective.com
  • Permanent Art & Design
    Permanent is a nationally recognized design & branding firm out of Minneapolis, MN. They've been a long term collaborator and strategic partner over the past couple years, and we'd recommend them for medium to large sized businesses/organizations who are serious about putting their best foot forward. In addition to design, Permanent offers strategy and marketing services, plus in-house and networked developers. For more information contact Joseph Belk joseph@permanentadg.com
  • Nate Thompson
    A freelancer, Nate reminds us of ourselves back in the day. Smart, communicative, and skilled, Nate is our recommendation for small to medium sized businesses/organizations who have sites built on Wordpress or other open source content management solutions. With a background in both design and development, he's a one-stop shop for most web-related tasks. You can get in touch at nate.a.thompson@gmail.com
  • Kevin DeBernardi
    Designer turned developer, Kevin is embarking on a freelance web career after working as the in-house designer at the Museum of the City of New York. He's adept at translating his design ideas into custom PHP code, and is constantly expanding his technical palette. Kevin's a good choice for projects that don't quite fit into the Wordpress mold, and that would benefit from a consistency of presence and vision from design through execution. Kevin can be reached at kevin@analoglifestyle.com