Our WordPress template displays the categories that a post is within. For example if a post is in the category Dogs and also the category Cats then those category links are displayed on the post's page.
Is it possible to output the links like this...
<a href="/category/cats" class="cats">Cats</a>
<a href="/category/cats" class="dogs">Dogs</a>
The reason is we'd like to style each category's link with a different color.
If so, how can I accomplish this?
Assuming this is on the single template, you can generate the links using something like this:
$categories = wp_get_post_terms( get_the_id(), 'category' );
if ( $categories ):
foreach ( $categories as $category ): ?>
<a href="<?php echo get_term_link( $category->term_id, 'category' ); ?>" class="<?php echo $category->slug; ?>"><?php echo $category->name; ?></a>
<?php endforeach;
endif;
The class will be generated from the category slug, so you can be sure that it will never have a space in it.