Make WordPress Core

Changeset 31271


Ignore:
Timestamp:
01/23/2015 03:40:31 PM (10 years ago)
Author:
boonebgorges
Message:

Add classes for custom taxonomy terms in get_post_class().

Props sillybean.
Fixes #16223.

Location:
trunk
Files:
1 added
1 edited

Legend:

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

    r31090 r31271  
    390390 * The class names are many. If the post is a sticky, then the 'sticky'
    391391 * class name. The class 'hentry' is always added to each post. If the post has a
    392  * post thumbnail, 'has-post-thumbnail' is added as a class. For each
    393  * category, the class will be added with 'category-' with category slug is
    394  * added. The tags are the same way as the categories with 'tag-' before the tag
    395  * slug. All classes are passed through the filter, 'post_class' with the list
    396  * of classes, followed by $class parameter value, with the post ID as the last
    397  * parameter.
     392 * post thumbnail, 'has-post-thumbnail' is added as a class. For each taxonomy that
     393 * the post belongs to, a class will be added of the format '{$taxonomy}-{$slug}' -
     394 * eg 'category-foo' or 'my_custom_taxonomy-bar'. The 'post_tag' taxonomy is a special
     395 * case; the class has the 'tag-' prefix instead of 'post_tag-'. All classes are
     396 * passed through the filter, 'post_class' with the list of classes, followed by
     397 * $class parameter value, with the post ID as the last parameter.
    398398 *
    399399 * @since 2.7.0
     400 * @since 4.2.0 Custom taxonomy classes were added.
    400401 *
    401402 * @param string|array $class One or more classes to add to the class list.
     
    447448    $classes[] = 'hentry';
    448449
    449     // Categories
    450     if ( is_object_in_taxonomy( $post->post_type, 'category' ) ) {
    451         foreach ( (array) get_the_category($post->ID) as $cat ) {
    452             if ( empty($cat->slug ) )
    453                 continue;
    454             $classes[] = 'category-' . sanitize_html_class($cat->slug, $cat->term_id);
    455         }
    456     }
    457 
    458     // Tags
    459     if ( is_object_in_taxonomy( $post->post_type, 'post_tag' ) ) {
    460         foreach ( (array) get_the_tags($post->ID) as $tag ) {
    461             if ( empty($tag->slug ) )
    462                 continue;
    463             $classes[] = 'tag-' . sanitize_html_class($tag->slug, $tag->term_id);
     450    // All public taxonomies
     451    $taxonomies = get_taxonomies( array( 'public' => true ) );
     452    foreach ( (array) $taxonomies as $taxonomy ) {
     453        if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
     454            foreach ( (array) get_the_terms( $post->ID, $taxonomy ) as $term ) {
     455                if ( empty( $term->slug ) ) {
     456                    continue;
     457                }
     458
     459                // 'post_tag' uses the 'tag' prefix for backward compatibility.
     460                if ( 'post_tag' == $taxonomy ) {
     461                    $classes[] = 'tag-' . sanitize_html_class( $term->slug, $term->term_id );
     462                } else {
     463                    $classes[] = sanitize_html_class( $taxonomy . '-' . $term->slug, $taxonomy . '-' . $term->term_id );
     464                }
     465            }
    464466        }
    465467    }
Note: See TracChangeset for help on using the changeset viewer.