0

i'm new in the wordpress developpment. i started coding my first theme and i want to dispay categories with color, each category has his own color. in wordpress i know that we have : category name - category link and i want to add category color for example.

Note : without plugin or costum field but i can add a lot of colors to css file if that's help.

  <div class="category">
        <a href="#">
     <span style="background: <?php echo $cats[0]->color ;?>"><?php echo $cats[0]->name; ?></span>
        </a> // this my idea i don't know if that's working or not
   </div>
   

exemple

2
  • Welcome. Might look into post_class() and use CSS class names instead of inline color styling with PHP in classic/hyprid themes.
    – birgire
    Jul 20, 2022 at 13:47
  • @birgire thank you for your reply yes that's help for just replacing the class name i want to set a color to each category and when i want to get the category name i will be able to get the category color too, for example red color for Food category
    – medempire
    Jul 20, 2022 at 14:32

1 Answer 1

0

I would add the category slug as a class to each category link, and change your class name to be more specific so other CSS doesn't conflict on accident:

<div class="category-link">
    <a href="#" class="<?= $cats[0]->slug ?>"><?= $cats[0]->name ?></a>
</div>

Then call the class with CSS:

.category-link .food {
    background-color: red;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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