How to get post category name in a wp-query->have_posts loop (the string only)
18,044
Somebody already though about it. You're looking for the post_class
function.
Doing:
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
Should give you, for example:
<div id="post-1" class="post-1 post type-post status-publish format-standard hentry category-one category-two tag-one tag-two">
EDIT:
If you want only the category name, then you have to specify witch category you need, as posts can have more than one. Apart from loop searches and verifications, if you want the first one, you would do:
$c = get_the_category();
// Name
echo $c[0]->cat_name;
// Slug
echo $c[0]->category_nicename;
Comments
-
devric almost 2 years
I want to get the string only
post_category_name
for each post in a whileWP_Query()->have_posts()
loop. e.g.: the following.<?php while (WP_Query()->have_posts()) : ?> <div class="post_category_name"></div> <?php endwhile; ?>
How do i get the category name for the loop, string only, without any element wrapping so i can use it as a
class="category"
.I've tried
the_category()
but it returns ali > a
item...