2

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>
  • 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 Mar 30 '15 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 Mar 30 '15 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 Mar 31 '15 at 18:47
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!

  • Hi Rachel, thank you for the through explanation. With each time WP_query loops will come a differing class / category. I did try this first category only but it didn't work. As I have a wp_query with multiple categories I would like to out put the category according to the post. Confusing I know! I would love to solve this one. Cheers for all of your help. – Max Jacobs Mar 30 '15 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.