How to get a category in a list item class

The first issue I’m seeing is that you’re trying to use the_category as a class. This won’t work as this function is intended to output the links to the categories assigned to that post. So it’s putting the href inside your class.

You can debug and see how it works by removing your li and the class. Then see how the_category works by itself. You can inspect it in your browser and see the markup. You’ll see the difference and why it won’t work with how you have it. So if you have a category called “apples” and “pears” on a post, and you have the_category( ' ' ); by itself in the loop, you should see something like this in your inspector.

<a href="http://yoursite.com/category/apples/" rel="category tag">Apples</a> 
<a href="http://yoursite.com/category/pears/" rel="category tag">Pears</a>

Now with your code like you have it as <li class="<?php the_category(' '); ?>></li>, you’ll see something like this in your inspector.

<li class="  ">This doesn't work</li>

So if you just want the category names to use them as classes, maybe you should try get_the_category instead. Also, what happens if there’s more than one category? Do you want them all in your class?

Assuming you only want the first category only, you’d write something like this instead:

<?php
$post_cat = get_the_category(); //get the category of this post
$cat_name = $post_cat[0]->cat_name; //get the name of the first category
?>

<li class="<?php echo $cat_name; ?>">Your first category is a class now.</li>

If you want something else, you’ll have to be more specific in your question.

The second problem I see is the markup. The <li> used to wrap your category with a class opens inside the loop and appears to close outside the loop after the endwhile. Perhaps putting the closing </li> before the endwhile will help as well.

Hope that helps!