^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_post_class()用法示例

get_post_class( string|array $class = '',  int|WP_Post $post_id = null )

Retrieves the classes for the post div as an array.


描述

The class names are many. If the post is a sticky, then the ‘sticky’ class name. The class ‘hentry’ is always added to each post. If the post has a post thumbnail, ‘has-post-thumbnail’ is added as a class. For each taxonomy that the post belongs to, a class will be added of the format ‘{$taxonomy}-{$slug}’ – eg ‘category-foo’ or ‘my_custom_taxonomy-bar’.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

The ‘post_tag’ taxonomy is a special case; the class has the ‘tag-‘ prefix instead of ‘post_tag-‘. All classes are passed through the filter, ‘post_class’, with the list of classes, followed by $class parameter value, with the post ID as the last parameter.


参数

$class

(string|array)
(Optional)
One or more classes to add to the class list.

Default value: ''

$post_id

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

Default value: null


返回值

(array) Array of classes.


源代码

File: wp-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_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 ) );
	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 classes for the current post.
	 *
	 * @since 2.7.0
	 *
	 * @param array $classes An array of post classes.
	 * @param array $class   An array of additional classes added to the post.
	 * @param int   $post_id The post ID.
	 */
	$classes = apply_filters( 'post_class', $classes, $class, $post->ID );

	return array_unique( $classes );
}

更新日志

Version 描述
4.2.0 Custom taxonomy classes were added.
2.7.0 Introduced.

相关函数

Uses

  • wp-includes/category-template.php:
    get_the_terms()
  • wp-includes/theme.php:
    current_theme_supports()
  • wp-includes/formatting.php:
    sanitize_html_class()
  • wp-includes/query.php:
    is_attachment()
  • wp-includes/query.php:
    is_home()
  • wp-includes/query.php:
    is_paged()
  • wp-includes/load.php:
    is_admin()
  • wp-includes/taxonomy.php:
    is_object_in_taxonomy()
  • wp-includes/taxonomy.php:
    get_taxonomies()
  • wp-includes/plugin.php:
    apply_filters()
  • wp-includes/post-thumbnail-template.php:
    has_post_thumbnail()
  • wp-includes/post-template.php:
    post_password_required()
  • wp-includes/post-template.php:
    post_class
  • wp-includes/post.php:
    is_sticky()
  • wp-includes/post.php:
    post_type_supports()
  • wp-includes/post.php:
    get_post()
  • wp-includes/post-formats.php:
    get_post_format()
  • wp-includes/load.php:
    is_wp_error()
  • Show 13 more uses


Used By

  • wp-admin/includes/class-wp-posts-list-table.php:
    WP_Posts_List_Table::single_row()
  • wp-includes/post-template.php:
    post_class()

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Default Usage
    Default example without params and global $post object available (or in the loop).

    
    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-[...]'
    }
    
  2. With Params
    Passing an array of your own classes.

    
    /* 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)
    	...
    */
    

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈