From 04216633b2fc6e21a458481af3bd0abf036e59a3 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 23 Jan 2015 15:41:22 +0000 Subject: [PATCH] Add classes for custom taxonomy terms in `get_post_class()`. Props sillybean. Fixes #16223. Built from https://develop.svn.wordpress.org/trunk@31271 git-svn-id: http://core.svn.wordpress.org/trunk@31252 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/post-template.php | 42 ++++++++++++++++++----------------- wp-includes/version.php | 2 +- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/wp-includes/post-template.php b/wp-includes/post-template.php index d6dfa322a3..d5a4009770 100644 --- a/wp-includes/post-template.php +++ b/wp-includes/post-template.php @@ -389,14 +389,15 @@ function post_class( $class = '', $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 - * 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. + * 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 '{$taxonomy}-{$slug}' - + * 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, 'post_class' with the list of classes, followed by + * $class parameter value, with the post ID as the last parameter. * * @since 2.7.0 + * @since 4.2.0 Custom taxonomy classes were added. * * @param string|array $class One or more classes to add to the class list. * @param int|WP_Post $post_id Optional. Post ID or post object. @@ -446,21 +447,22 @@ function get_post_class( $class = '', $post_id = null ) { // 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); - } - } + // All public taxonomies + $taxonomies = get_taxonomies( array( 'public' => true ) ); + foreach ( (array) $taxonomies as $taxonomy ) { + if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) ) { + foreach ( (array) get_the_terms( $post->ID, $taxonomy ) as $term ) { + if ( empty( $term->slug ) ) { + continue; + } - // 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); + // 'post_tag' uses the 'tag' prefix for backward compatibility. + if ( 'post_tag' == $taxonomy ) { + $classes[] = 'tag-' . sanitize_html_class( $term->slug, $term->term_id ); + } else { + $classes[] = sanitize_html_class( $taxonomy . '-' . $term->slug, $taxonomy . '-' . $term->term_id ); + } + } } } diff --git a/wp-includes/version.php b/wp-includes/version.php index a2f483e7d5..1efc944bb0 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.2-alpha-31270'; +$wp_version = '4.2-alpha-31271'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.