0
votes

I am displaying the featured image for all my posts on one page (the image then links to the individual post). I want to give each post from the same category the same class (which will be the name of the category slug), so for example, if a post is in a category called "cat1" I would like the div that contains the post to have the class "cat1" and so on.

So far I have this:

<article class="<?php get_the_category($post->ID)?>">

but this just returns blank, can someone tell me what I'm missing here please.

1

1 Answers

1
votes

The Codex is your friend:

// add category nicenames in body and post class
function so20621481_category_id_class($classes) {
    global $post;
    foreach((get_the_category($post->ID)) as $category)
        $classes[] = $category->category_nicename;
    return $classes;
}
add_filter('post_class', 'so20621481_category_id_class');

NB: for this to work, your article element should contain the post_class template tag:

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>