WordPress函数post_class()

描述:

自动输出当前文章/页面的class类信息

用法:

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

参数:

class

(string or array) (可选) 要添加到类属性的一个或多个class,用空格分隔。

默认值: null

$post_id

(int) (可选) 从循环外部调用此函数时使用的可选post ID

默认值: null

示例:

//下面的示例演示如何将post_类模板标记实现到主题中。
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

<div id="post-4564" class="post post-4564 category-48 category-dancing logged-in">

.post {
	/* styles for all posts */
}
.post-4564 {
	/* styles for only post ID number 4564 */
}
.category-dancing {
	/* styles for all posts within the category of dancing */
}

源文件:

function post_class( $class = '', $post_id = null ) {
    // Separates classes with a single space, collates classes for post DIV
    echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"';
}