wp query - How to get a category in a list item class

admin2025-01-07  4

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: ?>

    <p>There are no posts or pages here</p>

<?php endif; ?>

<?php wp_reset_postdata(); ?>

    </li>

</ul>

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: ?>

    <p>There are no posts or pages here</p>

<?php endif; ?>

<?php wp_reset_postdata(); ?>

    </li>

</ul>
Share Improve this question edited Mar 30, 2015 at 17:06 Pieter Goosen 55.4k23 gold badges115 silver badges209 bronze badges asked Mar 30, 2015 at 14:39 Max JacobsMax Jacobs 512 bronze badges 3
  • Hi Max, when you say the output is breaking, do you mean you're getting a PHP error or that it visually looks broken? From the markup you've provided, I'm seeing an opening and closing <ul> outside of the loop, and then a closing <li> outside the loop when the start of that <li> is inside the loop? There is also no beginning to the second <ul> for what I imagine is your categories in the loop. Perhaps re-working the html will help if it's a visual problem. It also appears that the_category function already wraps in an unordered list by default if you look at the codex. – RachieVee Commented Mar 30, 2015 at 15:34
  • Hi Rachel, I mean it looks like this on the page ?>"> as it it doesn't output this correctly <li class="<?php the_category(' '); ?>"> It not now a php error, more that I am not sure in how to do this! Thanks for any help!! :) – Max Jacobs Commented Mar 30, 2015 at 16:46
  • I'm also noticing you have a post__and parameter? I think you meant post__in? codex.wordpress.org/Class_Reference/… – RachieVee Commented Mar 31, 2015 at 18:47
Add a comment  | 

1 Answer 1

Reset to default 0

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!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256295a350.html

最新回复(0)