24 $_format = get_the_terms( $post->ID, 'post_format' ); |
24 $_format = get_the_terms( $post->ID, 'post_format' ); |
25 |
25 |
26 if ( empty( $_format ) ) |
26 if ( empty( $_format ) ) |
27 return false; |
27 return false; |
28 |
28 |
29 $format = array_shift( $_format ); |
29 $format = reset( $_format ); |
30 |
30 |
31 return str_replace('post-format-', '', $format->slug ); |
31 return str_replace('post-format-', '', $format->slug ); |
32 } |
32 } |
33 |
33 |
34 /** |
34 /** |
35 * Check if a post has a particular format |
35 * Check if a post has any of the given formats, or any format. |
36 * |
36 * |
37 * @since 3.1.0 |
37 * @since 3.1.0 |
38 * |
38 * |
39 * @uses has_term() |
39 * @param string|array $format Optional. The format or formats to check. |
40 * |
40 * @param object|int $post Optional. The post to check. If not supplied, defaults to the current post if used in the loop. |
41 * @param string|array $format The format or formats to check. |
41 * @return bool True if the post has any of the given formats (or any format, if no format specified), false otherwise. |
42 * @param object|int $post The post to check. If not supplied, defaults to the current post if used in the loop. |
42 */ |
43 * @return bool True if the post has the format, false otherwise. |
43 function has_post_format( $format = array(), $post = null ) { |
44 */ |
|
45 function has_post_format( $format, $post = null ) { |
|
46 if ( ! is_array( $format ) ) |
|
47 $format = array( $format ); |
|
48 |
|
49 $prefixed = array(); |
44 $prefixed = array(); |
50 foreach( $format as $single ) { |
45 |
51 $prefixed[] = 'post-format-' . sanitize_key( $single ); |
46 if ( $format ) { |
|
47 foreach ( (array) $format as $single ) { |
|
48 $prefixed[] = 'post-format-' . sanitize_key( $single ); |
|
49 } |
52 } |
50 } |
53 |
51 |
54 return has_term( $prefixed, 'post_format', $post ); |
52 return has_term( $prefixed, 'post_format', $post ); |
55 } |
53 } |
56 |
54 |
106 /** |
104 /** |
107 * Retrieves an array of post format slugs. |
105 * Retrieves an array of post format slugs. |
108 * |
106 * |
109 * @since 3.1.0 |
107 * @since 3.1.0 |
110 * |
108 * |
111 * @uses get_post_format_strings() |
|
112 * |
|
113 * @return array The array of post format slugs. |
109 * @return array The array of post format slugs. |
114 */ |
110 */ |
115 function get_post_format_slugs() { |
111 function get_post_format_slugs() { |
116 $slugs = array_keys( get_post_format_strings() ); |
112 $slugs = array_keys( get_post_format_strings() ); |
117 return array_combine( $slugs, $slugs ); |
113 return array_combine( $slugs, $slugs ); |
119 |
115 |
120 /** |
116 /** |
121 * Returns a pretty, translated version of a post format slug |
117 * Returns a pretty, translated version of a post format slug |
122 * |
118 * |
123 * @since 3.1.0 |
119 * @since 3.1.0 |
124 * |
|
125 * @uses get_post_format_strings() |
|
126 * |
120 * |
127 * @param string $slug A post format slug. |
121 * @param string $slug A post format slug. |
128 * @return string The translated post format name. |
122 * @return string The translated post format name. |
129 */ |
123 */ |
130 function get_post_format_string( $slug ) { |
124 function get_post_format_string( $slug ) { |
184 } else { |
177 } else { |
185 $link = remove_query_arg( 'post_format', $link ); |
178 $link = remove_query_arg( 'post_format', $link ); |
186 return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link ); |
179 return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link ); |
187 } |
180 } |
188 } |
181 } |
189 add_filter( 'term_link', '_post_format_link', 10, 3 ); |
|
190 |
182 |
191 /** |
183 /** |
192 * Remove the post format prefix from the name property of the term object created by get_term(). |
184 * Remove the post format prefix from the name property of the term object created by get_term(). |
193 * |
185 * |
194 * @access private |
186 * @access private |
198 if ( isset( $term->slug ) ) { |
190 if ( isset( $term->slug ) ) { |
199 $term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) ); |
191 $term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) ); |
200 } |
192 } |
201 return $term; |
193 return $term; |
202 } |
194 } |
203 add_filter( 'get_post_format', '_post_format_get_term' ); |
|
204 |
195 |
205 /** |
196 /** |
206 * Remove the post format prefix from the name property of the term objects created by get_terms(). |
197 * Remove the post format prefix from the name property of the term objects created by get_terms(). |
207 * |
198 * |
208 * @access private |
199 * @access private |