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(); ?>">

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.

  • Please see my updated answer.
    – tfrommen
    Commented Feb 6, 2014 at 10:09

1 Answer 1


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 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.

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

$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(); ?>">

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

  • Thanks TF, I've just edited my question above so now it hopefully makes more sense. Commented Feb 5, 2014 at 17:16
  • That's not it I'm afraid TF, in fact now we're just pulling the category and not the parent category. My issue is not with getting the parent as this is working great but with get_the_category(); not working for custom post types. Commented Feb 6, 2014 at 9:34
  • I've added a further explanation to what I'm trying to achieve to the question above. Hope it helps. Thanks for your time. Commented Feb 6, 2014 at 9:54
  • Sadly still only working for default post types. Could it be that get_cat_name won't work with custom post types? Commented Feb 6, 2014 at 10:22
  • TF, I may have found the route of my problem and will post an answer shortly. get_the_terms would appear to work but, looking more closely at the taxonomy it appears that 'category' is not always 'category' but sometimes 'product_cat' or 'course-category' and so was not being turned out. Commented Feb 6, 2014 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.