39 * @param array $visited Optional. Already linked to categories to prevent duplicates. |
39 * @param array $visited Optional. Already linked to categories to prevent duplicates. |
40 * @return string |
40 * @return string |
41 */ |
41 */ |
42 function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) { |
42 function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) { |
43 $chain = ''; |
43 $chain = ''; |
44 $parent = &get_category( $id ); |
44 $parent = get_category( $id ); |
45 if ( is_wp_error( $parent ) ) |
45 if ( is_wp_error( $parent ) ) |
46 return $parent; |
46 return $parent; |
47 |
47 |
48 if ( $nicename ) |
48 if ( $nicename ) |
49 $name = $parent->slug; |
49 $name = $parent->slug; |
71 * @param int $id Optional, default to current post ID. The post ID. |
71 * @param int $id Optional, default to current post ID. The post ID. |
72 * @return array |
72 * @return array |
73 */ |
73 */ |
74 function get_the_category( $id = false ) { |
74 function get_the_category( $id = false ) { |
75 $categories = get_the_terms( $id, 'category' ); |
75 $categories = get_the_terms( $id, 'category' ); |
76 if ( ! $categories ) |
76 if ( ! $categories || is_wp_error( $categories ) ) |
77 $categories = array(); |
77 $categories = array(); |
78 |
78 |
79 $categories = array_values( $categories ); |
79 $categories = array_values( $categories ); |
80 |
80 |
81 foreach ( array_keys( $categories ) as $key ) { |
81 foreach ( array_keys( $categories ) as $key ) { |
133 * @param int $cat_ID Category ID. |
133 * @param int $cat_ID Category ID. |
134 * @return string Category name. |
134 * @return string Category name. |
135 */ |
135 */ |
136 function get_the_category_by_ID( $cat_ID ) { |
136 function get_the_category_by_ID( $cat_ID ) { |
137 $cat_ID = (int) $cat_ID; |
137 $cat_ID = (int) $cat_ID; |
138 $category = &get_category( $cat_ID ); |
138 $category = get_category( $cat_ID ); |
139 if ( is_wp_error( $category ) ) |
139 if ( is_wp_error( $category ) ) |
140 return $category; |
140 return $category; |
141 return $category->name; |
141 return $category->name; |
142 } |
142 } |
143 |
143 |
1052 /** |
1052 /** |
1053 * Retrieve the terms of the taxonomy that are attached to the post. |
1053 * Retrieve the terms of the taxonomy that are attached to the post. |
1054 * |
1054 * |
1055 * @since 2.5.0 |
1055 * @since 2.5.0 |
1056 * |
1056 * |
1057 * @param int $id Post ID. |
1057 * @param mixed $post Post ID or object. |
1058 * @param string $taxonomy Taxonomy name. |
1058 * @param string $taxonomy Taxonomy name. |
1059 * @return array|bool False on failure. Array of term objects on success. |
1059 * @return array|bool False on failure. Array of term objects on success. |
1060 */ |
1060 */ |
1061 function get_the_terms( $id, $taxonomy ) { |
1061 function get_the_terms( $post, $taxonomy ) { |
1062 global $post; |
1062 if ( ! $post = get_post( $post ) ) |
1063 |
1063 return false; |
1064 $id = (int) $id; |
1064 |
1065 |
1065 $terms = get_object_term_cache( $post->ID, $taxonomy ); |
1066 if ( !$id ) { |
|
1067 if ( empty( $post->ID ) ) |
|
1068 return false; |
|
1069 else |
|
1070 $id = (int) $post->ID; |
|
1071 } |
|
1072 |
|
1073 $terms = get_object_term_cache( $id, $taxonomy ); |
|
1074 if ( false === $terms ) { |
1066 if ( false === $terms ) { |
1075 $terms = wp_get_object_terms( $id, $taxonomy ); |
1067 $terms = wp_get_object_terms( $post->ID, $taxonomy ); |
1076 wp_cache_add($id, $terms, $taxonomy . '_relationships'); |
1068 wp_cache_add($post->ID, $terms, $taxonomy . '_relationships'); |
1077 } |
1069 } |
1078 |
1070 |
1079 $terms = apply_filters( 'get_the_terms', $terms, $id, $taxonomy ); |
1071 $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy ); |
1080 |
1072 |
1081 if ( empty( $terms ) ) |
1073 if ( empty( $terms ) ) |
1082 return false; |
1074 return false; |
1083 |
1075 |
1084 return $terms; |
1076 return $terms; |