get_post_class()

最后更新于:2021-11-26 22:27:41

get_post_class( string|string[]$class=”, int|GC_Post$post_id=null)

Retrieves an array of the class names for the post container element.

参数

$class

(string|string[]) (Optional) Space-separated string or array of class names to add to the class list.

Default value: ”

$post_id

(int|GC_Post) (Optional) Post ID or post object.

Default value: null

响应

(string[]) Array of class names.

源文件

文件: gc-includes/post-template.php

function get_post_class( $class = '', $post_id = null ) {
	$post = get_post( $post_id );

	$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_gc_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 ) );
	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 );
}
var_dump( get_post_class() );
 
/*Output:*/
array() {
	0 => string 'post-[ID]' (length=7)
	1 => string '[post_type]' (length=4)
	2 => string 'type-[post_type]' (length=9)
	3 => string 'status-[post_status]' (length=14)
	4 => string 'format-[post_format]' (length=15)
	5 => string 'hentry' (length=6)
	6 => string 'category-[...]'
	X => string 'tag-[...]'
}
/* Optional:
global $post;
$postID = $post->ID;
 OR $postID = get_the_ID();
$postClass = get_post_class(array('my-class'));
 OR $postClass = get_post_class(array('my-class'), (int) $postID);
*/

$post_class = get_post_class( array( 'my-class' ) );
var_dump( $post_class );

/* Output:
array
	0 => string 'post-[ID]' (length=7)
	1 => string '[post_type]' (length=4)
	2 => string 'type-[post_type]' (length=9)
	3 => string 'status-[post_status]' (length=14)
	4 => string 'format-[post_format]' (length=15)
	5 => string 'hentry' (length=6)
	6 => string 'category-[...]'
	...
	X => string 'tag-[...]'
	...
	X+1 => string 'my-class' (length=8)
	...
*/