post_class( string|array $class = '', int|WP_Post $post_id = null )
Displays the classes for the post container element.
Description Description
Parameters Parameters
- $class
-
(string|array) (Optional) One or more classes to add to the class list.
Default value: ''
- $post_id
-
(int|WP_Post) (Optional) Post ID or post object. Defaults to the global
$post
.Default value: null
Source Source
File: wp-includes/post-template.php
function post_class( $class = '', $post_id = null ) { // Separates classes with a single space, collates classes for post DIV echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"'; }
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
2.7.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
Add more classes.
You can add a class to the
post_class
defaults:The above prints HTML with your added class and the defaults:
Use an array to add multiple classes:
To print
post_class
CSS classes for a post other then the current one, specify its ID (integer):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
post_class filter
You can also add classes using the
post_class
filter.A simple way to add multiple classes to the
post_class
defaults, is to just write them as a string argument:which will add those classes to the default class list.
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 assingle.php
):The output of the above prints this HTML (for a post in the ‘news’ category and a theme that supports Post Formats):
Using these CSS classes you can then style this specific post, or all posts assigned the same category (or post format):
Expand full source codeCollapse full source code