Title: post_class
Published: April 25, 2014
Last modified: February 24, 2026

---

# post_class( string|string[] $css_class, int|WP_Post $post = null )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/post_class/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/functions/post_class/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/post_class/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/post_class/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/post_class/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/post_class/?output_format=md#wp--skip-link--target)

Displays the classes for the post container element.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/post_class/?output_format=md#parameters)󠁿

 `$css_class`string|string[]optional

One or more classes to add to the class list.
 Default empty.

`$post`int|[WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)
optional

Post ID or post object. Defaults to the global `$post`.

Default:`null`

## 󠀁[Source](https://developer.wordpress.org/reference/functions/post_class/?output_format=md#source)󠁿

    ```php
    function post_class( $css_class = '', $post = null ) {
    	// Separates classes with a single space, collates classes for post DIV.
    	echo 'class="' . esc_attr( implode( ' ', get_post_class( $css_class, $post ) ) ) . '"';
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/post-template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/post-template.php#L465)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/post-template.php#L465-L468)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/post_class/?output_format=md#related)󠁿

| Uses | Description | 
| [get_post_class()](https://developer.wordpress.org/reference/functions/get_post_class/)`wp-includes/post-template.php` |

Retrieves an array of the class names for the post container element.

  | 
| [esc_attr()](https://developer.wordpress.org/reference/functions/esc_attr/)`wp-includes/formatting.php` |

Escaping for HTML attributes.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/post_class/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.7.0](https://developer.wordpress.org/reference/since/2.7.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/post_class/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 6 content](https://developer.wordpress.org/reference/functions/post_class/?output_format=md#comment-content-903)
 2.    [hearvox](http://hearingvoices.com)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/post_class/#comment-903)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fpost_class%2F%23comment-903)
     Vote results for this note: 4[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fpost_class%2F%23comment-903)
 4.  **Add more classes.**
      You can add a class to the `post_class` defaults:
 5.      ```php
         <div id="post-<?php the_ID(); ?>" <?php post_class( 'class-name' ); ?>>
         ```
     
 6.  The above prints HTML with your added class and the defaults:
      [html]<div id="
     post-4564" class="class-name post-4564 post type-post status-publish format-standard
     hentry category-news">[/html]
 7.  Use an array to add multiple classes:
 8.      ```php
         <?php
         $classes = array(
         	'class1',
         	'class2',
         	'class3',
         );
         ?>
     
         <div id="post-<?php the_ID(); ?>" <?php post_class( $classes ); ?>>
         ```
     
 9.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fpost_class%2F%3Freplytocom%3D903%23feedback-editor-903)
 10.  [Skip to note 7 content](https://developer.wordpress.org/reference/functions/post_class/?output_format=md#comment-content-904)
 11.   [hearvox](http://hearingvoices.com)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/post_class/#comment-904)
 12. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fpost_class%2F%23comment-904)
     Vote results for this note: 3[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fpost_class%2F%23comment-904)
 13. To print `post_class` CSS classes for a post other then the current one, specify
     its ID (integer):
 14.     ```php
         <?php post_class( '', 22 ); ?>
         ```
     
 15. The above prints (depending on category and tags):
      `class="post-22 post type-
     post status-publish format-standard hentry category-another-cat tag-tag1 tag-tag2`
 16.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fpost_class%2F%3Freplytocom%3D904%23feedback-editor-904)
 17.  [Skip to note 8 content](https://developer.wordpress.org/reference/functions/post_class/?output_format=md#comment-content-902)
 18.   [hearvox](http://hearingvoices.com)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/post_class/#comment-902)
 19. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fpost_class%2F%23comment-902)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fpost_class%2F%23comment-902)
 20. **Example of the template tag (and its default CSS classes).**
      This example shows
     the `post_class` template tag as commonly used in a theme file (such as `single.
     php`):
 21.     ```php
         <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
         ```
     
 22. The output of the above prints this HTML (for a post in the ‘news’ category and
     a theme that supports Post Formats):
      [html]<div id="post-4564" class="post-4564
     post type-post status-publish format-standard hentry category-news">[/html]
 23. Using these CSS classes you can then style this specific post, or all posts assigned
     the same category (or post format):
 24.     ```line-numbers
         .post {
         	/* Styles for all posts */
         }
     
         .post-4564 {
         	/* Styles for only this post (ID number 4564) */
         }
     
         .category-news {
         	/* Styles for all posts in the 'news' category  */
         }
     
         .format-standard {
         	/* Styles for all posts assigned the post-format of 'standard'  */
         }
         ```
     
 25.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fpost_class%2F%3Freplytocom%3D902%23feedback-editor-902)
 26.  [Skip to note 9 content](https://developer.wordpress.org/reference/functions/post_class/?output_format=md#comment-content-2524)
 27.   [Selrond](https://profiles.wordpress.org/selrond/)  [  8 years ago  ](https://developer.wordpress.org/reference/functions/post_class/#comment-2524)
 28. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fpost_class%2F%23comment-2524)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fpost_class%2F%23comment-2524)
 29. A simple way to add multiple classes to the `post_class` defaults, is to just 
     write them as a string argument:
 30.     ```php
         <div <?php post_class( 'class1 class2 class3' ); ?>>
         ```
     
 31. which will add those classes to the default class list.
 32.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fpost_class%2F%3Freplytocom%3D2524%23feedback-editor-2524)
 33.  [Skip to note 10 content](https://developer.wordpress.org/reference/functions/post_class/?output_format=md#comment-content-905)
 34.   [hearvox](http://hearingvoices.com)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/post_class/#comment-905)
 35. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fpost_class%2F%23comment-905)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fpost_class%2F%23comment-905)
 36. **post_class filter**
      You can also add classes using the [`post_class` filter](https://developer.wordpress.org/reference/hooks/post_class/).
 37.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fpost_class%2F%3Freplytocom%3D905%23feedback-editor-905)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fpost_class%2F)
before being able to contribute a note or feedback.