post_class( string|array $class = '', int|WP_Post $post_id = null )
描述
显示post div的类。
参数
$class
(string | array) (可选) 添加到类列表中的一个或多个类。
默认值: ''
$post_id
(int | WP_Post) (可选) Post ID或Post对象。默认为全局$post。
默认值:null
例子
此示例显示post_class了主题文件(如single.php)中常用的模板标签:
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
以上输出打印此HTML(对于“新闻”类别中的帖子和支持Post格式的主题):
<div id="post-4564" class="post-4564 post type-post status-publish format-standard hentry category-news">
使用这些CSS类,您可以对此特定的帖子进行风格化,或者分配相同类别(或帖子格式)的所有帖子:
.post {
/* Styles for all posts */
}
.post-4564 {
/* Styles for only this post (ID number 4564) */
}
.category-news {
/* Styles for all posts in the 'news' category */
}
.format-standard {
/* Styles for all posts assigned the post-format of 'standard' */
}
例子二
您可以将类添加到post_class默认值:
<div id="post-<?php the_ID(); ?>" <?php post_class( 'class-name' ); ?>>
以上打印HTML添加的类和默认值:
<div id="post-4564" class="class-name post-4564 post type-post status-publish format-standard hentry category-news">
使用数组添加多个类:
<?php
$classes = array(
'class1',
'class2',
'class3',
);
?>
<div id="post-<?php the_ID(); ?>" <?php post_class( $classes ); ?>>
例子三
要打印post_class其他类型的CSS类,然后是当前的,请指定其ID(整数):
以上打印(取决于类别和标签):
class="post-22 post type-post status-publish format-standard hentry category-another-cat tag-tag1 tag-tag2
源文件
wp-includes / post-template.php: get_post_class()

