WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Getting category slug from posts in the loop

[Resolved] Getting category slug from posts in the loop

  • Hello! I am trying to figure out a way to output the category slugs for each of the posts in the loop.

    My general loop looks like this:

    <?php
    // The Query
    $the_query = new WP_Query($args);
    
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();?>

    and I am trying to put each post in between <li>. I want to give each post a class within the <li> tag that consists of their category slugs (this is for JQuery purposes).
    I found this in a tutorial, but it doesn’t work for me:

    <li class="<?php foreach((get_the_category()) as $category) {
    echo get_cat_slug($category->cat_ID) . ' ';} ?> ">

    I think I have to modify something like this, but I’m not sure. help?

    <?php
    $category = get_the_category();
    $cat_=name =  $category[0]->cat_name;
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] Getting category slug from posts in the loop’ is closed to new replies.
Skip to toolbar