get_post_class

Retrieves the classes for the post div as an array.

Description

(array) get_post_class( (string) $class = '', (null) $post_id = null ); 

The class names are many. If the post is a sticky, then the sticky class name. The class hentry is always added to each post. If the post has a post thumbnail, has-post-thumbnail. is added as a class. For each taxonomy that the post belongs to, a class will be added of the format - - eg category-foo or my_custom_taxonomy-bar.

The post_tag taxonomy is a special case; the class has the tag- prefix instead of post_tag-. All classes are passed through the filter, , with the list of classes, followed by $class parameter value, with the post ID as the last parameter.

Returns (array)

Array of classes.

Parameters (2)

0. $class — Optional. (string) => ''
One or more classes to add to the class list.
1. $post_id — Optional. (null) => null
Post ID or post object.

Usage

  1. if ( !function_exists( 'get_post_class' ) ) { 
  2. require_once ABSPATH . WPINC . '/post-template.php'; 
  3.  
  4. // One or more classes to add to the class list. 
  5. $class = ''; 
  6.  
  7. // Optional. Post ID or post object. 
  8. $post_id = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_post_class($class, $post_id); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/post-template.php  
  1. function get_post_class( $class = '', $post_id = null ) { 
  2. $post = get_post( $post_id ); 
  3.  
  4. $classes = array(); 
  5.  
  6. if ( $class ) { 
  7. if ( ! is_array( $class ) ) { 
  8. $class = preg_split( '#\s+#', $class ); 
  9. $classes = array_map( 'esc_attr', $class ); 
  10. } else { 
  11. // Ensure that we always coerce class to being an array. 
  12. $class = array(); 
  13.  
  14. if ( ! $post ) { 
  15. return $classes; 
  16.  
  17. $classes[] = 'post-' . $post->ID; 
  18. if ( ! is_admin() ) 
  19. $classes[] = $post->post_type; 
  20. $classes[] = 'type-' . $post->post_type; 
  21. $classes[] = 'status-' . $post->post_status; 
  22.  
  23. // Post Format 
  24. if ( post_type_supports( $post->post_type, 'post-formats' ) ) { 
  25. $post_format = get_post_format( $post->ID ); 
  26.  
  27. if ( $post_format && !is_wp_error($post_format) ) 
  28. $classes[] = 'format-' . sanitize_html_class( $post_format ); 
  29. else 
  30. $classes[] = 'format-standard'; 
  31.  
  32. $post_password_required = post_password_required( $post->ID ); 
  33.  
  34. // Post requires password. 
  35. if ( $post_password_required ) { 
  36. $classes[] = 'post-password-required'; 
  37. } elseif ( ! empty( $post->post_password ) ) { 
  38. $classes[] = 'post-password-protected'; 
  39.  
  40. // Post thumbnails. 
  41. if ( current_theme_supports( 'post-thumbnails' ) && has_post_thumbnail( $post->ID ) && ! is_attachment( $post ) && ! $post_password_required ) { 
  42. $classes[] = 'has-post-thumbnail'; 
  43.  
  44. // sticky for Sticky Posts 
  45. if ( is_sticky( $post->ID ) ) { 
  46. if ( is_home() && ! is_paged() ) { 
  47. $classes[] = 'sticky'; 
  48. } elseif ( is_admin() ) { 
  49. $classes[] = 'status-sticky'; 
  50.  
  51. // hentry for hAtom compliance 
  52. $classes[] = 'hentry'; 
  53.  
  54. // All public taxonomies 
  55. $taxonomies = get_taxonomies( array( 'public' => true ) ); 
  56. foreach ( (array) $taxonomies as $taxonomy ) { 
  57. if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) ) { 
  58. foreach ( (array) get_the_terms( $post->ID, $taxonomy ) as $term ) { 
  59. if ( empty( $term->slug ) ) { 
  60. continue; 
  61.  
  62. $term_class = sanitize_html_class( $term->slug, $term->term_id ); 
  63. if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) { 
  64. $term_class = $term->term_id; 
  65.  
  66. // 'post_tag' uses the 'tag' prefix for backward compatibility. 
  67. if ( 'post_tag' == $taxonomy ) { 
  68. $classes[] = 'tag-' . $term_class; 
  69. } else { 
  70. $classes[] = sanitize_html_class( $taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id ); 
  71.  
  72. $classes = array_map( 'esc_attr', $classes ); 
  73.  
  74. /** 
  75. * Filters the list of CSS classes for the current post. 
  76. * @since 2.7.0 
  77. * @param array $classes An array of post classes. 
  78. * @param array $class An array of additional classes added to the post. 
  79. * @param int $post_id The post ID. 
  80. */ 
  81. $classes = apply_filters( 'post_class', $classes, $class, $post->ID ); 
  82.  
  83. return array_unique( $classes );