WordPress post_class() Function

Definition
In WordPress, post_class() prints a class attribute for one post. You put it on the article or div that wraps each post in The Loop.
The classes include the post ID, post type, status, format, and its categories and tags. This gives you many hooks for CSS.
Syntax
A few ways to call post_class().
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
| Call | What it does |
|---|
post_class() | Print the default classes. |
post_class( 'card' ) | Add your own class. |
post_class( 'card', 12 ) | Use the classes of the post with ID 12. |
get_post_class() | Return the classes as an array instead of printing. |
It prints the whole class="..." attribute, so do not add class= yourself on the same tag.
Full declaration
post_class( string|string[] $css_class = '', int|WP_Post $post = null ): void
Parameters
| Parameter | Description |
|---|
$css_class | string|string[]. One or more extra classes. Default is empty. |
$post | int|WP_Post. The post ID or object. Default is the current post. |
Return Value
| Return Value | Description |
|---|
| void | Nothing is returned. The class attribute is printed. |
WordPress Examples
Three short examples. Each one shows the code and what it prints.
Example 1 — Add it to each post
Use it on the article tag inside The Loop.
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2><?php the_title(); ?></h2>
</article>
<?php endwhile; ?>
<article id="post-12" class="post-12 post type-post status-publish format-standard hentry category-news">
<h2>Hello World</h2>
</article>
How It Works
the_ID() makes a unique id for each post.post_class() prints classes for the post type, status, and categories.- Use these classes in your CSS, for example
.category-news.
Example 2 — Add your own class
Add a class to every post card.
<article <?php post_class( 'card' ); ?>>
<article class="post-12 post type-post status-publish format-standard hentry category-news card">
How It Works
- Your class is added after the default classes.
- Pass an array to add several classes.
- You can now style
.card once for all posts.
Example 3 — Add a class by rule
Use the filter to add a class only for posts with a thumbnail.
<?php
function mytheme_post_classes( $classes ) {
if ( has_post_thumbnail() ) {
$classes[] = 'has-image';
}
return $classes;
}
add_filter( 'post_class', 'mytheme_post_classes' );
<article class="post-12 post type-post status-publish format-standard has-post-thumbnail hentry category-news has-image">
How It Works
- The
post_class filter gets the list of classes. has_post_thumbnail() checks for a featured image.- Only posts with an image get
has-image.
Frequently Asked Questions
It prints a class attribute for one post with classes such as post ID, post type, status, and terms.
On the article or div that wraps each post inside The Loop.
post_class() styles one post. body_class() styles the whole page and goes on the body tag.
Pass it as an argument, like post_class( 'card' ), or use the post_class filter.
Yes. Use get_post_class(). It returns an array.
Browse all post functions
Open the functions index for simple guides with examples and FAQs.
Post Functions Index →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
15 people found this page helpful