Forums

[resolved] Add class to Category name function (3 posts)

  1. jumust
    Member
    Posted 4 months ago #

    I use the function below to get category name and then output category link as well. Is there a way that I can add a class to the category name only (want to put background color only for the category name )?

    <?php
    						$category = get_the_category();
    						echo $category[0]->cat_name;
    					?>
    						<a href="<?php echo get_category_link($category[0]->cat_ID); ?>" title="<?php echo $category[0]->cat_name; ?>">Leggi tutti</a>
    [....]
  2. alchymyth
    The Sweeper
    Posted 4 months ago #

    you could try to change this line:

    echo $category[0]->cat_name;

    into:

    echo '<span class="cat-'.$category[0]->category_nicename.'">'.$category[0]->cat_name.'</span>';

    which would give you the css class .cat-your-category-slug

  3. jumust
    Member
    Posted 4 months ago #

    Thanks it worked!

Reply

You must log in to post.

About this Topic