Support » Fixing WordPress » Styling Post Categories

  • Resolved ads1018

    (@ads1018)


    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.

Viewing 10 replies - 1 through 10 (of 10 total)
  • esmi

    (@esmi)

    Forum Moderator

    That depends upon how you are generating the category links.

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

    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.

    Anyone have any clue?

    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> ';
    }
    
    ?>

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

    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

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

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

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Styling Post Categories’ is closed to new replies.