Ready to get started?Download WordPress


Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get post class

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.


Retrieve the classes for the post div as an array.

Many class names can be added to posts. For example, if the post is a sticky, then the 'sticky' class name is added. The class 'hentry' is always added to each post. For each category, the class that will be added is 'category-' with the 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.

When dealing with posts outside The Loop (so in a second or custom loop), you might expect to call this get_post_class function instead of post_class, similar to the get_ alternatives to most other post functions. This is not the case though, you still need the post_class() function outside the loop, but with the post_id as 2nd parameter.


<?php get_post_class($class$post_id); ?>


(string or array) (optional) A string or array of classes to add to the return value
Default: '' (empty string)
(int) (optional) An optional post ID
Default: null

Return Value

$classes (array) 
An array of class names.
Get the object post from get_post($post_id); so, if empty return value of get_post (typically invalid $post_id), return an empty array of classes ($classes).


Default example without params and global $post object available (or in the loop).
$postClass = 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-[...]' } ?>
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);

$postClass get_post_class(array('my-class'));

/* Output:
  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)

Source Code

get_post_class() is located in wp-includes/post-template.php.


  • Since: 2.7



See also index of Function Reference and index of Template Tags.
This article is marked as in need of editing. You can help Codex by editing it.