S
S
StaticArt2020-08-25 15:30:19
WordPress
StaticArt, 2020-08-25 15:30:19

How to display different color of displayed categories in Wordpress?

The category name looks like this:

<a class="post-category-color-text" style="background:#36c942" href="#">
<?php $category = get_the_category();  echo $category[0]->cat_name; ?></a>

How can I make the background change with different category names?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-08-25
@apisklov

You can add a class that is equal to the category slug and css for each class has its own background.

<?php $category = get_the_category(); ?>
<a class="post-category-color-text <?php echo $category[0]->slug; ?>" style="background:#36c942" href="#">
<?php echo $category[0]->cat_name; ?>
</a>

/* Пример  */
/* Для slug = animals */
.animals{
  background: #eee;
}
/* Для slug = birds */
.birds{
  background: #ccc;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question