• Why won’t the_category() statement work in the following code?

    <div id="style-<?php the_category(); ?>">
    blablabla all kind of posts within Politics
    </div>

    It drives me nuts. I wanna style the div element according to his category. Something like:

    div#style-Politics {
    color: blue;
    }

    Any help is appreciated!

Viewing 1 replies (of 1 total)
  • Probably should use get_the_category which returns all the categories on a post so you need to pick which ‘category’ to use.

    <?php
    $cats = get_the_category();
    if ($cats) {
      foreach($cats as $cat) {
        echo $cat->slug .' or '. $cat->cat_name;
      } //foreach ($cats
    //or this which is the first category found
    echo $cats[0]->slug;
    }
    
    ?>
Viewing 1 replies (of 1 total)

The topic ‘the_category() wont work!’ is closed to new replies.