get_post_class( string|string[] $css_class = '', int|WP_Post|null $post = null ): string[]
- Since
- 2.7.0, 4.2.0
- Source
wp-includes/post-template.php:494
Retrieves an array of the class names for the post container element.
Description
The class names are many: <ul> <li>If the post has a post thumbnail,
has-post-thumbnail is added as a class.</li> <li>If the post is sticky, then the sticky class name is added.</li> <li>The class hentry is always added to each post.</li> <li>For each taxonomy that the post belongs to, a class will be added of the format {$taxonomy}-{$slug}, e.g. 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-.</li> </ul> All class names are passed through the filter, 'post_class', followed by $css_class parameter value, with the post ID as the last parameter.Parameters
$css_classstring|string[]optional- Space-separated string or array of class names to add to the class list. Default empty.Default:
'' $postint|WP_Post|nulloptional- Post ID or post object.Default:
null
Return
string[]- Array of class names.
Hooks fired · 2
2 hooks fire while get_post_class() runs, in this order:
- apply_filters( post_class_taxonomies )filterline 572 (+78 into the body)
Filters the taxonomies to generate classes for each individual term.
- apply_filters( post_class )filterline 607 (+113 into the body)
Filters the list of CSS class names for the current post.
Uses · 17
- get_post()Retrieves post data given a post ID or post object.
- is_admin()Determines whether the current request is for an administrative interface page.
- post_type_supports()Checks a post type's support for a given feature.
- get_post_format()Retrieve the format slug for a post
- is_wp_error()Checks whether the given variable is a WordPress Error.
- sanitize_html_class()Sanitizes an HTML classname to ensure it only contains valid characters.
- post_password_required()Determines whether the post requires password and whether a correct password has been provided.
- current_theme_supports()Checks a theme's support for a given feature.
- has_post_thumbnail()Determines whether a post has an image attached.
- is_attachment()Determines whether the query is for an existing attachment page.
- is_sticky()Determines whether a post is sticky.
- is_home()Determines whether the query is for the blog homepage.
Show all 17
- is_paged()Determines whether the query is for a paged result and not for the first page.
- get_taxonomies()Retrieves a list of registered taxonomy names or objects.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- is_object_in_taxonomy()Determines if the given object type is associated with the given taxonomy.
- get_the_terms()Retrieves the terms of the taxonomy that are attached to the post.
Used by · 4
- WP_Posts_List_Table::single_row()
- WP_REST_Posts_Controller::prepare_item_for_response()Prepares a single post output for response.
- post_class()Displays the classes for the post container element.
- render_block_core_post_template()Renders the `core/post-template` block on the server.
Source
function get_post_class( $css_class = '', $post = null ) { $post = get_post( $post ); $classes = array(); if ( $css_class ) { if ( ! is_array( $css_class ) ) { $css_class = preg_split( '#\s+#', $css_class ); } $classes = array_map( 'esc_attr', $css_class ); } else { // Ensure that we always coerce class to being an array. $css_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[] $css_class An array of additional class names added to the post. */ $taxonomies = apply_filters( 'post_class_taxonomies', $taxonomies, $post->ID, $classes, $css_class );History
Introduced in 2.7.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
7.0.4
Parameter
$post retyped from int|WP_Post to int|WP_Post|null.verified against source4.2.0
Custom taxonomy class names were added.from the docblock
2.7.0
Introduced.from the docblock
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/post-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.