post_class() WordPress Function

The post_class() function is a utility function that helps with building classes for individual posts. It is useful for creating CSS classes that can be applied to specific posts, making it easier to style individual posts without affecting other posts on the site.

post_class( string|string[] $class = '', int|WP_Post $post_id = null ) #

Displays the classes for the post container element.


Parameters

$class

(string|string[])(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


Top ↑

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="' . esc_attr( implode( ' ', get_post_class( $class, $post_id ) ) ) . '"';
}


Top ↑

Changelog

Changelog
VersionDescription
2.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.