Codex tools: Log in
Languages: English • 日本語 • (Add your language)
WordPress Themes have a template tag for the post HTML tag which will help theme authors to style more effectively with CSS. The Template Tag is called get_post_class. This function returns different post container classes which can be added, typically, in the index.php, single.php, and other template files featuring post content, typically in the HTML <div id="post"> tag.
Contents |
Retrieve the classes for the post div as an array.
The class names are add are many. If the post is a sticky, then the 'sticky' class name. The class 'hentry' is always added to each post. For each category, the class will be added with 'category-' with category slug is added. The tags are the same way as the categories with 'tag-' before the tag slug. 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.
<?php get_post_class($class, $post_id); ?>
<?php
$postClass = get_post_class();
var_dump($postClass);
/*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-[...]' } ?>
<?php
/* 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);
*/
$postClass = get_post_class(array('my-class'));
var_dump($postClass);
/* 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)
...
*/
?>
get_post_class() is located in wp-includes/post-template.php.
body_class(), next_image_link(), next_post_link(), next_posts_link(), post_class(), post_password_required(), posts_nav_link(), previous_image_link(), previous_post_link(), previous_posts_link(), single_post_title, sticky_class(), the_category(), the_category_rss(), the_content(), the_content_rss(), the_excerpt(), the_excerpt_rss(), the_ID(), the_meta(), the_shortlink(), the_tags(), the_title(), the_title_attribute(), the_title_rss(), wp_link_pages()