get_post_class

函数
get_post_class ( $class = '', $post = null )
参数
  • (string|string[]) $class Space-separated string or array of class names to add to the class list.
    Required:
    Default: (empty)
  • (int|WP_Post) $post Optional. Post ID or post object.
    Required:
    Default: null
返回值
  • (string[]) Array of class names.
定义位置
相关方法
post_classwp_get_post_catsget_body_classget_postsget_comment_class
引入
2.7.0
弃用
-

get_post_class函数用于检索应用于post元素的CSS类数组。它有两个可选参数:第一个参数是附加CSS类的数组,第二个参数是文章的ID。

为post容器元素检索一个类名数组。

类名有很多。如果文章是一个粘性的,那么’粘性’类名。’hentry’类总是被添加到每个文章中。如果文章有一个 缩略图,’has-post-thumbnail’就被作为一个类添加。对于文章所属的每个分类法,将添加一个格式为'{$taxonomy}-{$slug}’的类–例如’category-foo’或’my_custom_taxonomy-bar’。

post_tag”分类法是一个特例;该类有”tag-“前缀而不是”post_tag-“。所有的类名都通过过滤器,{@see ‘post_class’},类名列表,后面是$class参数值,文章ID是最后一个参数。

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

常见问题

FAQs
查看更多 >