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
This commit is contained in:
Boone Gorges 2015-01-23 15:41:22 +00:00
parent d45a650846
commit 04216633b2
2 changed files with 23 additions and 21 deletions

View File

@ -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' * 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 * 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 * post thumbnail, 'has-post-thumbnail' is added as a class. For each taxonomy that
* category, the class will be added with 'category-' with category slug is * the post belongs to, a class will be added of the format '{$taxonomy}-{$slug}' -
* added. The tags are the same way as the categories with 'tag-' before the tag * eg 'category-foo' or 'my_custom_taxonomy-bar'. The 'post_tag' taxonomy is a special
* slug. All classes are passed through the filter, 'post_class' with the list * case; the class has the 'tag-' prefix instead of 'post_tag-'. All classes are
* of classes, followed by $class parameter value, with the post ID as the last * passed through the filter, 'post_class' with the list of classes, followed by
* parameter. * $class parameter value, with the post ID as the last parameter.
* *
* @since 2.7.0 * @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 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. * @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 // hentry for hAtom compliance
$classes[] = 'hentry'; $classes[] = 'hentry';
// Categories // All public taxonomies
if ( is_object_in_taxonomy( $post->post_type, 'category' ) ) { $taxonomies = get_taxonomies( array( 'public' => true ) );
foreach ( (array) get_the_category($post->ID) as $cat ) { foreach ( (array) $taxonomies as $taxonomy ) {
if ( empty($cat->slug ) ) if ( is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
continue; foreach ( (array) get_the_terms( $post->ID, $taxonomy ) as $term ) {
$classes[] = 'category-' . sanitize_html_class($cat->slug, $cat->term_id); if ( empty( $term->slug ) ) {
} continue;
} }
// Tags // 'post_tag' uses the 'tag' prefix for backward compatibility.
if ( is_object_in_taxonomy( $post->post_type, 'post_tag' ) ) { if ( 'post_tag' == $taxonomy ) {
foreach ( (array) get_the_tags($post->ID) as $tag ) { $classes[] = 'tag-' . sanitize_html_class( $term->slug, $term->term_id );
if ( empty($tag->slug ) ) } else {
continue; $classes[] = sanitize_html_class( $taxonomy . '-' . $term->slug, $taxonomy . '-' . $term->term_id );
$classes[] = 'tag-' . sanitize_html_class($tag->slug, $tag->term_id); }
}
} }
} }

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @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. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.