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.
This is expected (or at least: designed) behavior.
Here is a portion of the Codex on that:
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:
If you also want to add some post classes, do it like this: