Switch language

wpseek.com
A WordPress-centric search engine for devs and theme authors




get_post_class [ WordPress Function ]

get_post_class ( $class = '', $post_id = null )
Parameters:
  • (string|array) $class One or more classes to add to the class list.
  • (int) $post_id An optional post ID.
Returns:
  • (array) Array of classes.
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

More ...

User discussions [ wordpress.org ]

0 User Note(s)

None yet. Be the first!

Add New ...



HTML5 Powered with CSS3 / Styling, Performance & Integration, and Semantics