I would like to style a category link depending on its ID, for instance, if the category ID is 2, then I want the link to be pink.

I cannot find a way to do the same thing to links. What type of solutions are available? Please be detailed in your reply as my coding knowledge is limited. Thanks a lot :)

  • 1
    what category links are you referring to? the ones from a category menu or categories widget? or the one from a post? – Michael Mar 23 '13 at 18:53

Open the page which is rendering the category posts, and in the appropriate place (before running the loop) write the following code

$catid = get_cat_id();
$color = "#222"; //default;
if ($catid == $pinkCategoryId) $color = "#8B0A50";
else if ($catid == $redCategoryId) $color = "#FF0000";

<style type = 'text/css'>
a { color: <?php echo $color;?>; }

Now adjust the code block above according to your need

You can also use it by category name instead of category id, just change the get_cat_id to get_the_category() which will return the current category name. Then you can perform the rest of the task as is

| improve this answer | |
  • I am not insisting, but when an answer solves your problem please select the answer as accepted one. – Hasin Hayder Mar 23 '13 at 18:12
  • Thanks for your help, but should i replace $pinkCategoryId by an ID ? Here is how i call the category link on the page: <?php $categories = get_the_category(); $separator = ' '; $output = ''; if($categories){ foreach($categories as $category) { $output .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator; } echo trim($output, $separator); } ?> – Ludo Mar 23 '13 at 18:17
  • you can use category name too, see the answer above - I have edited – Hasin Hayder Mar 23 '13 at 18:19
  • ok thanks, but the color doesn't work. could you please tell me if my code is correct ? <?php $catid = get_the_category(); if ($catid == "typographie") $color = "#8B0A50"; else if ($catid == "signalétique") $color = "#FF0000"; else if ($catid == "édition") $color = "black"; else if ($catid == "identité visuelle") $color = "red"; ?> <style type = 'text/css'> a { color: <?php echo $color;?>; } </style> <? foreach($catid as $category) { echo $category->cat_name . ' '; } ?> – Ludo Mar 24 '13 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.