get_post_class [ WordPress Function ]
| Parameters: |
|
| Returns: |
|
| Defined at: |
|
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.
Source
<?php
function get_post_class( $class = '', $post_id = null ) {
$post = get_post($post_id);
$classes = array();
if ( empty($post) )
return $classes;
$classes[] = 'post-' . $post->ID;
$classes[] = $post->post_type;
$classes[] = 'type-' . $post->post_type;
$classes[] = 'status-' . $post->post_status;
// Post Format
if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
$post_format = get_post_format( $post->ID );
if ( $post_format && !is_wp_error($post_format) )
$classes[] = 'format-' . sanitize_html_class( $post_format );
else
$classes[] = 'format-standard';
}
// post requires password
if ( post_password_required($post->ID) )
$classes[] = 'post-password-required';
// sticky for Sticky Posts
if ( is_sticky($post->ID) && is_home() && !is_paged() )
$classes[] = 'sticky';
// hentry for hAtom compliance
$classes[] = 'hentry';
// Categories
if ( is_object_in_taxonomy( $post->post_type, 'category' ) ) {
foreach ( (array) get_the_category($post->ID) as $cat ) {
if ( empty($cat->slug ) )
continue;
$classes[] = 'category-' . sanitize_html_class($cat->slug, $cat->term_id);
}
}
// Tags
if ( is_object_in_taxonomy( $post->post_type, 'post_tag' ) ) {
foreach ( (array) get_the_tags($post->ID) as $tag ) {
if ( empty($tag->slug ) )
continue;
$classes[] = 'tag-' . sanitize_html_class($tag->slug, $tag->term_id);
}
}
if ( !empty($class) ) {
if ( !is_array( $class ) )
$class = preg_split('#\s+#', $class);
$classes = array_merge($classes, $class);
}
$classes = array_map('esc_attr', $classes);
return apply_filters('post_class', $classes, $class, $post->ID);
}
?>
Examples [ wp-snippets.com ]
Top Google Search Results
- Function Reference/get post class « WordPress Codex
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 ...
codex.wordpress.org - get_post_class | A HitchHackers guide through WordPress
Feb 12, 2011 ... 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 ...
hitchhackerguide.com - PHPXRef 0.7 : WordPress : Function Reference: get_post_class()
Function and Method Cross Reference. get_post_class(). Defined at: /wp- includes/post-template.php -> line 298. Referenced 2 times: ...
phpxref.ftwr.co.uk - get_post_class (WordPress Function) - WPSeek.com
WordPress lookup for get_post_class, a WordPress Function. wpseek.com is a WordPress-centric search tool for developers and theme authors.
wpseek.com