Forums

unique css classes for each category link (10 posts)

  1. wyndot
    Member
    Posted 1 year ago #

    I'm looked all over for an answer to this and I can't seem to find one, hopefully someone knows how to make this work.

    I'm trying to give each of my category name links in the post meta a different class so that each can have a unique background image. I know that wp_list_categories gives each li a unique class based on the category id, but it doesn't look like I can use that to show the categories for a single post.

    Thanks ion advance for any ideas.

    Geoff

  2. Frumph
    Member
    Posted 1 year ago #

    .. they should already exist as classes if the theme is using <?php post_class(); ?>

    http://codex.wordpress.org/Function_Reference/post_class

  3. Frumph
    Member
    Posted 1 year ago #

    It's possible you might need to describe the section you want each class to have a different background, what I posted above is for the post itself.

  4. wyndot
    Member
    Posted 1 year ago #

    Thanks for the quick response.

    I don't actually want the whole post to have a different background.

    I'm building a portfolio and each project has some custom taxonomies listed that describe the project. I basically want to add the program icons in the tools category. If each link has its own class I can do that as a bg image. See the following image:

    http://www.geoffponnath.com/wp-content/themes/gportfolio/images/example.jpg

    heres my code i'm using to list the taxonmies now:

    <h3>Skills</h3>
    <ul class="taxonomy"><?php the_terms( $post->ID, 'skill', '<li>', '</li><li>','</li>' ); ?></ul>
    <h3>Tools</h3>
    <ul class="taxonomy"><?php the_terms( $post->ID, 'tool', '<li>', '</li><li>','</li>' ); ?></ul>
  5. Frumph
    Member
    Posted 1 year ago #

    I do have an answer for this, writing it up right now

    Do you want it only to list the categories that the current post is in right? with css that befits it right?

  6. wyndot
    Member
    Posted 1 year ago #

    Thanks!

    Yep, just for that current post, with a unique class for each category (or taxonomy) name.

  7. Frumph
    Member
    Posted 1 year ago #

    yeah i'm writing something up that uses

    $categories = &get_term_by('id', $post->ID, 'category');

    that will get all the categories of a post and then write it out for you in the style you want, need a few minutes to finish it ;/

    unless of course you want to do it with that lead in on how to ;)

  8. Frumph
    Member
    Posted 1 year ago #

    [removed bad link, see post below]

    Here's a widget for ya.

    basically what it does it will get the categories for the post, if none exist display no categories found, otherwise it will format it out with

    echo '<li class="cat-'.$cat->slug.'">'.$cat->name.'</li>'."\r\n";

    Which cat-<slugname> of the category so you can use that to do a background image then margin-left: it the pixel amount of the image

    It uses $cats = get_the_category( $post->ID ); so that it only grabs the categories of the current post for ya.

    Probably need to add an if (is_single()) { wrapper on it so that it doesn't display on the home page or pages archives etc.. and only on single pages.

  9. wyndot
    Member
    Posted 1 year ago #

    Thanks, that sounds great, I'll give it a try!

  10. Frumph
    Member
    Posted 1 year ago #

    http://wp.pastebin.com/Q2vVmdf2

    Here, this is all fixed up, including clickable links

Topic Closed

This topic has been closed to new replies.

About this Topic