Category as Class for Custom Post Type

This is expected (or at least: designed) behavior.

Here is a portion of the Codex on that:

The post_class CSS classes appear based upon the post pageview Conditional Tags as follows.

Category
Category template files and pageviews displaying posts feature the class selectors: post post-id category-ID category-name

Of course, you can hook a custom filter function to the post_class filter.

// EDIT
According to your updated question, here is the updated code:

<?php
$cat = wp_get_post_terms(get_the_ID(), 'category');
$parentCatName = get_cat_name($cat[0]->parent);
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> data-category="<?php echo $parentCatName; ?>" data-title="<?php the_title(); ?>">
[...]
</article>

If you also want to add some post classes, do it like this:

post_class('category-'.$parentCatName);

Leave a Comment