WordPress 函数 post_class()
作为WordPress主题开发者,如果需要给文章增加css类,可以使用post_class()函数,wordpress还会同时增加默认的类。只能在the_loop中使用。 注意:该函数会直接输出添加的类名,使用get_post_class()返回添加的类名。
函数使用
向其他的诸如 header_image、wp_title这样的 WordPress 标签函数一样,不带 get 的函数通常是会直接显示出来而不返回任何值。
<post id="post-<?php the_ID(); ?>" <?php post_class(); ?> > <?php the_content ;?> </post>
是的,也许你已经注意到了,使用 post_class 函数时我们甚至不需要这样去写clss=“<?php post_class(); ?>”。
实例结果
结果如下
<post id="post-888" class="post-888 post type-post status-publish format-standard hentry category-2 tag-wordpress" > 文章内容 </post>
其默认的参数根据文章的类型不同可能包含下面的一个或多个类名:
- post-id
- post
- attachment
- sticky
- hentry
- category-name
- tag-name
- format-name
总结
以使用为主的函数讲完了,下面照旧给出函数源代码:想要了解更多关于该函数,以及get_post_class函数请关注后期文章。
/**
* Display the classes for the post div.
*
* @since 2.7.0
*
* @param string|array $class One or more classes to add to the class list.
* @param int $post_id An optional post ID.
*/
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 ) ) . '"';
