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);
Related Posts:
- Get Custom post with ID
- Archive page…limiting posts per page
- Adding custom post type to loop
- Best way to style first post differently?
- How do I write this SQL statement for Posts written in last 24 hours?
- Using new WP_Query in shortcode in a custom field causes the main post content to not display
- Taxonomy posts on Archive page
- WooCommerce – Show orders for a specific product ? [closed]
- How to append element after thumbnail
- Posts navigation in custom post type single.php not working
- Display custom post on home page based on a metabox selection
- Why does using WP_Query inside a shortcode in an elementor page cause the arguments for WP_Query to get malformed?
- Lists Posts from CPT that match the Title of the Page
- How would I create a shortcode to display a custom post within a page or regular post?
- get_template_part() – post-meta not working?
- Filter post by current 2 differents users id
- How to call custom single post?
- How to connect two custom post types with nested loops
- Only show posts from a certain category?
- Custom Post Type Loop throws 500 error when used in widget
- Custom loop with multiple taxonomy queries
- Paging doesn’t work?
- What is the recommended / best way to do this: Event calendar post/type in a block-based theme?
- Group CPT posts by custom taxonomy
- Problem: wp_query outputs all images on site
- The loop seems stuck to a single (now deleted) post
- Custom Post Types strange pagination problem
- Custom post type conditional in loop
- Display custom post type posts first, then default posts
- Targeting custom post type
- Including all post id’s of a custom post type into an array
- Multiple Custom Post Type queries, how to DRY it up
- Main query not querying any posts in custom taxonomy template
- Custom WP_Query doesn’t display all posts
- Query & the_content() is showing in the header
- Listing custom post types on archive page with array
- Pagination not working on custom query on a page
- Loop through posts of only 2 statuses
- Add more than one custom post type to wordpress home page post loop
- Custom wordpress loop
- Trying to add a class to post links
- get_terms() parent, child and grandchild
- Use post in multiple places on a page with multiple posts
- List all Custom Post Type posts excluding certain Taxnomy term
- pre_get_posts works in post type archive but not in single post
- Loop and add Specific Categories and Products Images
- Custom PT, Taxonomys and wordpress query
- How can I increase the post count for custom post types only?
- Custom post type pagination error
- Add custom post type to query
- Page that lists publications by classifying them by taxonomy
- posts_per_page in custom WP_Query does not override “Reading” settings?
- Custom post type paging return home page
- when looping through custom post data not appearing
- Order posts alphabetically with numbers but some of the posts has numbers in the title
- Multiple Loops In Tabs Only Displaying First Loop
- How to only get the content of post page while looping?
- SEARCH QUERIES – REVERSE OUTPUT
- Error in the page of a CPT file in WordPress
- ACF meta_key and meta_value break loop
- post_type incorrect for custom post type
- Skip latest 3 posts from loop
- Staggering featured post using ‘sticky’
- Multiple Custom Post Type in Taxonomy Archive Causing White Screen
- Adding a Section for Visitors
- Why is my loop not populating the page with my custom taxonomy terms from my custom post type?
- Show parent category and subcategory once in while loop
- List all posts associated under custom taxonomy
- How to build Custom Post Nav Menu inside of the Loop
- Loop to display parent categories in custom taxonomy
- Sort loop by custom field from different post type
- next_posts_link() always generates second page link on custom post type
- Display related post content and custom field content
- Show posts for current taxonomy
- 2 custom posts types, in their own columns, one pagination for both
- WordPress loop, show only one post per custom field
- How To Loop Through list with Custom Post Types
- How to Fix Custom WordPress Loop with Pagination on Single Post View?
- Multiple loops for custom post type to spread data across rows
- Pagination on custom post types
- Custom Post Type + Category archive
- Loop multiple taxonomy in custom post
- $post breaking container loop
- Post data in separate divs with incrementing class using WP_Query
- Redirection loop error after login from a custom post type post
- Posts without featured image using other post’s featured image
- Get result from Custom Field in Custom Post type
- Nested loop called with shortcode duplicating the content above the main loop
- Custom post-type’s pagination not working in category.php
- Pulling in a particular post into a single page based on the post id passed into the code
- why get_post_meta is returning 0?
- Post_type doesn’t work for widgets?
- How do I correctly query posts from a post ID?
- Get Posts From A Certain Post Type In An ID Array
- Show search for data extracted from metabox
- why does my post loop break the page?
- display all posts in current category
- Get all active posts that are tied to a custom taxonomy for a custom post type
- WordPress search results grouped by post type
- How to print different informations for different post types inside The Loop?