Question

I've only just realised that custom post types don't appear to output the category as a class in the loop, via post_class and I wonder if anyone can suggest a work around. Is this something that should be declared when I establish my post type or is this an extension of post_class?

For example, on a post of type post the class outputs something like: class="post-50434 post type-post status-publish format-standard hentry category-my-lovely-category tag-some-ace-tag" but for my custom post type the category doesn't appear.

-- EDIT --

Here's a bit of my code which might help to clarify what I'm trying to do - firstly though, I'm not actually trying to get the class onto the body but onto the article in my loop:

<?php $cat = get_the_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>

The trouble that I'm having is retrieving the custom post type category info in the same way as I can for the default post type.

-- EDIT --

Sorry, perhaps a little background to what I'm trying to achieve will help - I'll try to keep it brief:

  • I'm using Isotope masonry layout

  • I wish to filter results by a 'data-category' attribute added to each post during the loop

  • My categories for several post types (post, product, course) are grouped under common parent categories

  • In my loop I therefor want to populate that 'data-category' field with the parent category

My problem arises as I can only get the category, and in turn the parent category, for the default custom post type, using get_the_category.

I hope this is making sense.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top