How to get a category in a list item class

I am need to know how would I get the category to be dynamically input in a WP_Query

This is the code I am using:

li class="?php the_category(' ')?"

but the output is breaking.

This is all the code

 ul id="portfolio-list"

?php

$args = array(
    'post_type' = 'post',
    'post__and' = array( 15, 14, 17, 13, 10, 8, 12, 11, 9, 16),
);

$the_query = new WP_Query( $args );
?

?php if ( $the_query-have_posts() ) :? 


    ?php while ( $the_query-have_posts() ) : $the_query-the_post(); ?

  li class="?php the_category(' '); ?"


?php the_post_thumbnail( $size, $attr ); ? 

    ?php the_title(); ?

    br /

    ?php //the_category('cat-slug'); ?

?php endwhile; else: ?

    pThere are no posts or pages here/p

?php endif; ?

?php wp_reset_postdata(); ?

    /li

/ul

Topic list wp-query Wordpress

Category Web


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!

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.