WordPress.org

Ready to get started?Download WordPress

Forums

Styling categories classes (8 posts)

  1. ads1018
    Member
    Posted 4 years ago #

    If I have multiple categories assigned to a post, I'd like to be able to style each category link individually. For some reason, WordPress doesn't assign individual classes to the categories. By using the '<?php the_category' tag all the class names get clustered into one like this:

    <div class="post-253 post hentry category-devils category-nets"

    Any ideas on how to target each category class?

  2. stuartathompson
    Member
    Posted 4 years ago #

    Looks like .category-devils and .category-nets can be manipulated individually, like:

    .category-devils{
    color:red;
    } 
    
    .category-nets{
    color:blue;
    }
  3. ads1018
    Member
    Posted 4 years ago #

    That's what initially thought but it doesn't work. The styling would only get applied to one category because each link isn't given it's own class.

  4. ads1018
    Member
    Posted 4 years ago #

    Here's what the html output looks like:

    <div class="post-255 post sticky hentry category-nets category-yankees" class="post" id="post-255"> 
    
    <a href="http://nysportsmill.mu/blog/category/nets/" title="View all posts in nets" rel="category tag">nets</a>,
    <a href="http://nysportsmill.mu/blog/category/yankees/" title="View all posts in yankees" rel="category tag">yankees</a>

    Anyone have any clue how to target/style those links?

  5. ads1018
    Member
    Posted 4 years ago #

    Anyone have a clue?

  6. Darrell Schauss
    Member
    Posted 4 years ago #

    I looked into the default the_category() which ultimately uses the function get_the_category_list where in wordpress core files I don't see any spot for injecting other attributes such as a class by using filters.

    Maybe try doing it manually.
    $categories = wp_get_post_categories($post->ID);
    Then loop through the $categories array and generate your own links how you want. I haven't tried this out so Im not sure how much information it will give you such as the URL, etc.

  7. ads1018
    Member
    Posted 4 years ago #

    Unfortunately, I haven't taught myself php yet. I only get the basics. Would you mind showing me how it would look after I loop through the $categories array?

  8. MichaelH
    Member
    Posted 4 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic