Make WordPress Core


Ignore:
Timestamp:
02/02/2009 07:21:38 PM (15 years ago)
Author:
ryan
Message:

body_class(). Props ionfish. fixes #7939

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post-template.php

    r10459 r10485  
    337337
    338338    return apply_filters('post_class', $classes, $class, $post_id);
     339}
     340
     341/**
     342 * Display the classes for the body element.
     343 *
     344 * @since 2.8.0
     345 *
     346 * @param string|array $class One or more classes to add to the class list.
     347 */
     348function body_class( $class = '' ) {
     349    // Separates classes with a single space, collates classes for body element
     350    echo 'class="' . join( ' ', get_body_class( $class ) ) . '"';
     351}
     352
     353/**
     354 * Retrieve the classes for the body element as an array.
     355 *
     356 * @since 2.8.0
     357 *
     358 * @param string|array $class One or more classes to add to the class list.
     359 * @return array Array of classes.
     360 */
     361function get_body_class( $class = '' ) {
     362    global $wp_query, $current_user;
     363   
     364    $classes = array();
     365   
     366    if ( 'rtl' == get_bloginfo('text_direction') )
     367        $classes[] = 'rtl';
     368   
     369    if ( is_front_page() )
     370        $classes[] = 'home';
     371    if ( is_home() )
     372        $classes[] = 'blog';
     373    if ( is_archive() )
     374        $classes[] = 'archive';
     375    if ( is_date() )
     376        $classes[] = 'date';
     377    if ( is_search() )
     378        $classes[] = 'search';
     379    if ( is_paged() )
     380        $classes[] = 'paged';
     381    if ( is_attachment() )
     382        $classes[] = 'attachment';
     383    if ( is_404() )
     384        $classes[] = 'error404';
     385   
     386    if ( is_single() ) {
     387        the_post();
     388       
     389        $postID = $wp_query->post->ID;
     390        $classes[] = 'single postid-' . $postID;
     391       
     392        if ( is_attachment() ) {
     393            $mime_type = get_post_mime_type();
     394            $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
     395            $classes[] = 'attachmentid-' . $postID . ' attachment-' . str_replace( $mime_prefix, "", "$mime_type" );
     396        }
     397       
     398        rewind_posts();
     399    } elseif ( is_archive() ) {
     400        if ( is_author() ) {
     401            $author = $wp_query->get_queried_object();
     402            $classes[] = 'author';
     403            $classes[] = 'author-' . $author->user_nicename;
     404        } elseif ( is_category() ) {
     405            $cat = $wp_query->get_queried_object();
     406            $classes[] = 'category';
     407            $classes[] = 'category-' . $cat->slug;
     408        } elseif ( is_tag() ) {
     409            $tags = $wp_query->get_queried_object();
     410            $classes[] = 'tag';
     411            $classes[] = 'tag-' . $tags->slug;
     412        }
     413    } elseif ( is_page() ) {
     414        the_post();
     415       
     416        $pageID = $wp_query->post->ID;
     417        $page_children = wp_list_pages("child_of=$pageID&echo=0");
     418       
     419        if ( $page_children )
     420            $classes[] = 'page-parent';
     421       
     422        if ( $wp_query->post->post_parent )
     423            $classes[] = 'page-child parent-pageid-' . $wp_query->post->post_parent;
     424       
     425        if ( is_page_template() )
     426            $classes[] = 'page-template page-template-' . str_replace( '.php', '-php', get_post_meta( $pageID, '_wp_page_template', true ) );
     427       
     428        rewind_posts();
     429    } elseif ( is_search() ) {
     430        the_post();
     431       
     432        if ( have_posts() )
     433            $classes[] = 'search-results';
     434        else
     435            $classes[] = 'search-no-results';
     436       
     437        rewind_posts();
     438    }
     439   
     440    if ( is_user_logged_in() )
     441        $classes[] = 'logged-in';
     442   
     443    $page = $wp_query->get('page');
     444   
     445    if ( !$page || $page < 2)
     446        $page = $wp_query->get('paged');
     447   
     448    if ( $page && $page > 1 ) {
     449        $classes[] = 'paged-' . $page;
     450       
     451        if ( is_single() )
     452            $classes[] = 'single-paged-' . $page;
     453        elseif ( is_page() )
     454            $classes[] = 'page-paged-' . $page;
     455        elseif ( is_category() )
     456            $classes[] = 'category-paged-' . $page;
     457        elseif ( is_tag() )
     458            $classes[] = 'tag-paged-' . $page;
     459        elseif ( is_date() )
     460            $classes[] = 'date-paged-' . $page;
     461        elseif ( is_author() )
     462            $classes[] = 'author-paged-' . $page;
     463        elseif ( is_search() )
     464            $classes[] = 'search-paged-' . $page;
     465    }
     466   
     467    if ( !empty($class) ) {
     468        if ( !is_array( $class ) )
     469            $class = preg_split('#\s+#', $class);
     470        $classes = array_merge($classes, $class);
     471    }
     472   
     473    return apply_filters('body_class', $classes, $class);
    339474}
    340475
Note: See TracChangeset for help on using the changeset viewer.