mirror of
git://develop.git.wordpress.org/
synced 2025-02-24 08:33:35 +01:00
Add classes for custom taxonomy terms in get_post_class()
.
Props sillybean. Fixes #16223. git-svn-id: https://develop.svn.wordpress.org/trunk@31271 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
10d3b42211
commit
8ec0792557
@ -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);
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
67
tests/phpunit/tests/post/getPostClass.php
Normal file
67
tests/phpunit/tests/post/getPostClass.php
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group post
|
||||||
|
* @covers ::get_post_class
|
||||||
|
*/
|
||||||
|
class Tests_Post_GetPostClass extends WP_UnitTestCase {
|
||||||
|
protected $post_id;
|
||||||
|
|
||||||
|
public function setUp() {
|
||||||
|
parent::setUp();
|
||||||
|
$this->post_id = $this->factory->post->create();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_with_tags() {
|
||||||
|
wp_set_post_terms( $this->post_id, array( 'foo', 'bar' ), 'post_tag' );
|
||||||
|
|
||||||
|
$found = get_post_class( '', $this->post_id );
|
||||||
|
|
||||||
|
$this->assertContains( 'tag-foo', $found );
|
||||||
|
$this->assertContains( 'tag-bar', $found );
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_with_categories() {
|
||||||
|
$cats = $this->factory->category->create_many( 2 );
|
||||||
|
wp_set_post_terms( $this->post_id, $cats, 'category' );
|
||||||
|
|
||||||
|
$cat0 = get_term( $cats[0], 'category' );
|
||||||
|
$cat1 = get_term( $cats[1], 'category' );
|
||||||
|
|
||||||
|
$found = get_post_class( '', $this->post_id );
|
||||||
|
|
||||||
|
$this->assertContains( 'category-' . $cat0->slug, $found );
|
||||||
|
$this->assertContains( 'category-' . $cat1->slug, $found );
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_with_custom_taxonomy() {
|
||||||
|
register_taxonomy( 'wptests_tax', 'post' );
|
||||||
|
wp_set_post_terms( $this->post_id, array( 'foo', 'bar' ), 'wptests_tax' );
|
||||||
|
|
||||||
|
$found = get_post_class( '', $this->post_id );
|
||||||
|
|
||||||
|
$this->assertContains( 'wptests_tax-foo', $found );
|
||||||
|
$this->assertContains( 'wptests_tax-bar', $found );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group cache
|
||||||
|
*/
|
||||||
|
public function test_taxonomy_classes_hit_cache() {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
register_taxonomy( 'wptests_tax', 'post' );
|
||||||
|
wp_set_post_terms( $this->post_id, array( 'foo', 'bar' ), 'wptests_tax' );
|
||||||
|
wp_set_post_terms( $this->post_id, array( 'footag', 'bartag' ), 'post_tag' );
|
||||||
|
|
||||||
|
// Prime cache, including meta cache, which is used by get_post_class().
|
||||||
|
update_object_term_cache( $this->post_id, 'post' );
|
||||||
|
update_meta_cache( 'post', $this->post_id );
|
||||||
|
|
||||||
|
$num_queries = $wpdb->num_queries;
|
||||||
|
|
||||||
|
$found = get_post_class( '', $this->post_id );
|
||||||
|
|
||||||
|
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user