On this page
get_post_class( string|string[] $class = '', int|WP_Post $post = null ): string[]
Retrieves an array of the class names for the post container element.
Description
The class names are many. If the post is a sticky, then the ‘sticky’ class name. The class ‘hentry’ is always added to each post. If the post has a post thumbnail, ‘has-post-thumbnail’ is added as a class. For each taxonomy that the post belongs to, a class will be added of the format ‘{$taxonomy}-{$slug}’ – eg ‘category-foo’ or ‘my_custom_taxonomy-bar’.
The ‘post_tag’ taxonomy is a special case; the class has the ‘tag-‘ prefix instead of ‘post_tag-‘. All class names are passed through the filter, ‘post_class’, with the list of class names, followed by $class parameter value, with the post ID as the last parameter.
Parameters
$classstring|string[] Optional-
Space-separated string or array of class names to add to the class list.
Default:
'' $postint|WP_Post Optional-
Post ID or post object.
Default:
null
Return
string[] Array of class names.
Source
File: wp-includes/post-template.php. View all references
function get_post_class( $class = '', $post = null ) {
$post = get_post( $post );
$classes = array();
if ( $class ) {
if ( ! is_array( $class ) ) {
$class = preg_split( '#\s+#', $class );
}
$classes = array_map( 'esc_attr', $class );
} else {
// Ensure that we always coerce class to being an array.
$class = array();
}
if ( ! $post ) {
return $classes;
}
$classes[] = 'post-' . $post->ID;
if ( ! is_admin() ) {
$classes[] = $post->post_type;
}
$classes[] = 'type-' . $post->post_type;
$classes[] = 'status-' . $post->post_status;
// Post Format.
if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
$post_format = get_post_format( $post->ID );
if ( $post_format && ! is_wp_error( $post_format ) ) {
$classes[] = 'format-' . sanitize_html_class( $post_format );
} else {
$classes[] = 'format-standard';
}
}
$post_password_required = post_password_required( $post->ID );
// Post requires password.
if ( $post_password_required ) {
$classes[] = 'post-password-required';
} elseif ( ! empty( $post->post_password ) ) {
$classes[] = 'post-password-protected';
}
// Post thumbnails.
if ( current_theme_supports( 'post-thumbnails' ) && has_post_thumbnail( $post->ID ) && ! is_attachment( $post ) && ! $post_password_required ) {
$classes[] = 'has-post-thumbnail';
}
// Sticky for Sticky Posts.
if ( is_sticky( $post->ID ) ) {
if ( is_home() && ! is_paged() ) {
$classes[] = 'sticky';
} elseif ( is_admin() ) {
$classes[] = 'status-sticky';
}
}
// hentry for hAtom compliance.
$classes[] = 'hentry';
// All public taxonomies.
$taxonomies = get_taxonomies( array( 'public' => true ) );
/**
* Filters the taxonomies to generate classes for each individual term.
*
* Default is all public taxonomies registered to the post type.
*
* @since 6.1.0
*
* @param string[] $taxonomies List of all taxonomy names to generate classes for.
* @param int $post_id The post ID.
* @param string[] $classes An array of post class names.
* @param string[] $class An array of additional class names added to the post.
*/
$taxonomies = apply_filters( 'post_class_taxonomies', $taxonomies, $post->ID, $classes, $class );
foreach ( (array) $taxonomies as $taxonomy ) {
if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
foreach ( (array) get_the_terms( $post->ID, $taxonomy ) as $term ) {
if ( empty( $term->slug ) ) {
continue;
}
$term_class = sanitize_html_class( $term->slug, $term->term_id );
if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
$term_class = $term->term_id;
}
// 'post_tag' uses the 'tag' prefix for backward compatibility.
if ( 'post_tag' === $taxonomy ) {
$classes[] = 'tag-' . $term_class;
} else {
$classes[] = sanitize_html_class( $taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id );
}
}
}
}
$classes = array_map( 'esc_attr', $classes );
/**
* Filters the list of CSS class names for the current post.
*
* @since 2.7.0
*
* @param string[] $classes An array of post class names.
* @param string[] $class An array of additional class names added to the post.
* @param int $post_id The post ID.
*/
$classes = apply_filters( 'post_class', $classes, $class, $post->ID );
return array_unique( $classes );
}
Hooks
- apply_filters( 'post_class',
string[] $classes ,string[] $class ,int $post_id ) -
Filters the list of CSS class names for the current post.
- apply_filters( 'post_class_taxonomies',
string[] $taxonomies ,int $post_id ,string[] $classes ,string[] $class ) -
Filters the taxonomies to generate classes for each individual term.
Related
Uses
| Uses | Description |
|---|---|
| get_the_terms() wp-includes/category-template.php | Retrieves the terms of the taxonomy that are attached to the post. |
| has_post_thumbnail() wp-includes/post-thumbnail-template.php | Determines whether a post has an image attached. |
| get_post_format() wp-includes/post-formats.php | Retrieve the format slug for a post |
| post_type_supports() wp-includes/post.php | Checks a post type’s support for a given feature. |
| is_sticky() wp-includes/post.php | Determines whether a post is sticky. |
| post_password_required() wp-includes/post-template.php | Determines whether the post requires password and whether a correct password has been provided. |
| get_taxonomies() wp-includes/taxonomy.php | Retrieves a list of registered taxonomy names or objects. |
| is_object_in_taxonomy() wp-includes/taxonomy.php | Determines if the given object type is associated with the given taxonomy. |
| is_home() wp-includes/query.php | Determines whether the query is for the blog homepage. |
| is_paged() wp-includes/query.php | Determines whether the query is for a paged result and not for the first page. |
| is_attachment() wp-includes/query.php | Determines whether the query is for an existing attachment page. |
| sanitize_html_class() wp-includes/formatting.php | Sanitizes an HTML classname to ensure it only contains valid characters. |
| current_theme_supports() wp-includes/theme.php | Checks a theme’s support for a given feature. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| is_admin() wp-includes/load.php | Determines whether the current request is for an administrative interface page. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
Used By
| Used By | Description |
|---|---|
| WP_Posts_List_Table::single_row() wp-admin/includes/class-wp-posts-list-table.php | |
| post_class() wp-includes/post-template.php | Displays the classes for the post container element. |
Changelog
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_post_class