WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Styling Post Categories (11 posts)

  1. ads1018
    Member
    Posted 4 years ago #

    Let's say a post is assigned two separate categories. Is it possible to style each individual category link that belongs to that post?

    Seems like a simple task, but I'm not sure its possible.

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    That depends upon how you are generating the category links.

  3. ads1018
    Member
    Posted 4 years ago #

    I'm using the_category tag within the loop. Is there any other way?

  4. ads1018
    Member
    Posted 4 years ago #

    There's not much written about the_category tag. Seems like a no brainer to be able to style multiple category links in a post but no one seems to be able to do it.

  5. ads1018
    Member
    Posted 4 years ago #

    Anyone have any clue?

  6. MichaelH
    Member
    Posted 4 years ago #

    For use in The Loop to do what you want with it:

    <?php
    foreach((get_the_category()) as $category) {
        echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ';
    }
    
    ?>
  7. ads1018
    Member
    Posted 4 years ago #

    Thanks, but this still doesn't generate class id's for each category in the post.

  8. MichaelH
    Member
    Posted 4 years ago #

    What HTML do you want generated for these two categories two categories on each of these posts.:

    post 1
    category xyz
    category abc

    post 2
    category xyz
    category mlv

  9. ads1018
    Member
    Posted 4 years ago #

    Something along these lines:

    <ul class="post-categories"> 
    
    	<li class="xyz"><a href="http://mysite.com/blog/category/xyz/" title="View all posts in xyz" rel="category tag">xyz</a></li> 
    
    	<li class="abc"><a href="http://mysite.com/blog/category/abc/" title="View all posts in abc" rel="category tag">abc</a></li>
    </ul>
    <ul class="post-categories"> 
    
    	<li class="xyz"><a href="http://mysite.com/blog/category/xyz/" title="View all posts in xyz" rel="category tag">xyz</a></li> 
    
    	<li class="mlv"><a href="http://mysite.com/blog/category/mlv/" title="View all posts in mlv" rel="category tag">mlv</a></li>
    </ul>
  10. MichaelH
    Member
    Posted 4 years ago #

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

    Related:
    CSS
    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy

  11. ads1018
    Member
    Posted 4 years ago #

    Brilliant. Worked like a charm. Thanks for directing me to those links too. Can't emphasize enough how much stress you relieved!

Topic Closed

This topic has been closed to new replies.

About this Topic