get_post_class()WP 2.7.0

Gets CSS classes as an array that need to be used in the post for output in the HTML tag.

The final array of classes is passed through the filter post_class, use it when you need to change the displayed classes depending on different conditions.

To immediately output the classes on the screen as a string, use post_class()

Where and what classes are added
  • hentry, post-{$post_id}, type-{$post_type}, status-{$post_status}, {$post_type} — are output for all posts. The parts in brackets are replaced with the corresponding variable of the current post;
    {$post_type} is NOT output in the admin panel...

  • sticky — if this is a sticky post, it is only displayed on the is_home() page. In the admin panel, it is displayed as status-sticky;

  • has-post-thumbnail — if the post has a thumbnail;

  • format-{post format} — if support for formats is enabled. If the format is not specified, it will output format-standard.

  • post-password-required — for password-protected posts.

  • {$taxonomy}-{$slug} — if the post belongs to a taxonomy. $taxonomy will be replaced with the name of the taxonomy, and $slug with the name of the term (element of the taxonomy). For example: category-blog;
    The exception here is the taxonomy post_tag for which the prefix will be tag-, not post_tag-. For example: tag-novosti.
Used By: post_class()
1 time — 0.00616 sec (very slow) | 50000 times — 10.72 sec (slow) | PHP 7.0.2, WP 4.4.1
Hooks from the function

Returns

String[]. An array in which the elements are the names of the classes.

Usage

get_post_class( $class, $post_id );
$class(string/array)
The classes that need to be added to the list.
Default: ''
$post_id(int/WP_Post)
The ID or object of the post whose classes need to be obtained.
Default: null

Examples

0

#1 Output demonstration

$classes = get_post_class();
print_r( $classes );

/*
Array
(
	[0] => post-219
	[1] => post
	[2] => type-post
	[3] => status-publish
	[4] => format-standard
	[5] => has-post-thumbnail
	[6] => hentry
	[7] => category-blog
	[8] => tag-novosti
)
*/
0

#2 Let's output all classes as a string

This code can be used as a replacement for post_class():

<div class="<?= join( ' ', get_post_class() ) ?>">
0

#3 Adding our own classes

// as string
$classes = get_post_class( 'foo bar' );

// or as array
$classes = get_post_class( [ 'foo', 'bar' ] );

/* 
Both examples will add to the resulting array

Array
(
	[0] => foo
	[1] => bar
	[2] => post-19
)
*/
0

#4 Delete unnecessary class

Suppose we need to get rid of the class hentry, then use this filter before calling get_post_class() or post_class():

add_filter( 'post_class', 'remove_hentry' );

function remove_hentry( $classes ) {

	$unset = array( 'hentry' ); // you can add more

	return array_diff( $classes, $unset );
}

Notes

  • Since version 3.1, support for post formats has been introduced.
  • Since version 4.2, support for taxonomies has been introduced.

Changelog

Since 2.7.0 Introduced.
Since 4.2.0 Custom taxonomy class names were added.

get_post_class() code WP 7.0

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 );

	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[] $css_class An array of additional class names added to the post.
	 * @param int      $post_id   The post ID.
	 */
	$classes = apply_filters( 'post_class', $classes, $css_class, $post->ID );

	$classes = array_unique( $classes );
	$classes = array_values( $classes );

	return $classes;
}