author | ymh <ymh.work@gmail.com> |
Tue, 27 Sep 2022 16:37:53 +0200 | |
changeset 19 | 3d72ae0968f4 |
parent 18 | be944660c56a |
child 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* Taxonomy API: Core category-specific template tags |
0 | 4 |
* |
5 |
* @package WordPress |
|
6 |
* @subpackage Template |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7 |
* @since 1.2.0 |
0 | 8 |
*/ |
9 |
||
10 |
/** |
|
16 | 11 |
* Retrieves category link URL. |
0 | 12 |
* |
13 |
* @since 1.0.0 |
|
16 | 14 |
* |
0 | 15 |
* @see get_term_link() |
16 |
* |
|
17 |
* @param int|object $category Category ID or object. |
|
18 |
* @return string Link on success, empty string if category does not exist. |
|
19 |
*/ |
|
20 |
function get_category_link( $category ) { |
|
9 | 21 |
if ( ! is_object( $category ) ) { |
0 | 22 |
$category = (int) $category; |
9 | 23 |
} |
0 | 24 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
25 |
$category = get_term_link( $category ); |
0 | 26 |
|
9 | 27 |
if ( is_wp_error( $category ) ) { |
0 | 28 |
return ''; |
9 | 29 |
} |
0 | 30 |
|
31 |
return $category; |
|
32 |
} |
|
33 |
||
34 |
/** |
|
16 | 35 |
* Retrieves category parents with separator. |
0 | 36 |
* |
37 |
* @since 1.2.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
38 |
* @since 4.8.0 The `$visited` parameter was deprecated and renamed to `$deprecated`. |
0 | 39 |
* |
16 | 40 |
* @param int $category_id Category ID. |
41 |
* @param bool $link Optional. Whether to format with link. Default false. |
|
42 |
* @param string $separator Optional. How to separate categories. Default '/'. |
|
43 |
* @param bool $nicename Optional. Whether to use nice name for display. Default false. |
|
44 |
* @param array $deprecated Not used. |
|
0 | 45 |
* @return string|WP_Error A list of category parents on success, WP_Error on failure. |
46 |
*/ |
|
16 | 47 |
function get_category_parents( $category_id, $link = false, $separator = '/', $nicename = false, $deprecated = array() ) { |
0 | 48 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
49 |
if ( ! empty( $deprecated ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
50 |
_deprecated_argument( __FUNCTION__, '4.8.0' ); |
0 | 51 |
} |
52 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
53 |
$format = $nicename ? 'slug' : 'name'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
54 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
55 |
$args = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
56 |
'separator' => $separator, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
'link' => $link, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
'format' => $format, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
59 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
60 |
|
16 | 61 |
return get_term_parents_list( $category_id, 'category', $args ); |
0 | 62 |
} |
63 |
||
64 |
/** |
|
16 | 65 |
* Retrieves post categories. |
0 | 66 |
* |
16 | 67 |
* This tag may be used outside The Loop by passing a post ID as the parameter. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
68 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
69 |
* Note: This function only returns results from the default "category" taxonomy. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
70 |
* For custom taxonomies use get_the_terms(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
71 |
* |
0 | 72 |
* @since 0.71 |
73 |
* |
|
16 | 74 |
* @param int $post_id Optional. The post ID. Defaults to current post ID. |
9 | 75 |
* @return WP_Term[] Array of WP_Term objects, one for each category assigned to the post. |
0 | 76 |
*/ |
16 | 77 |
function get_the_category( $post_id = false ) { |
78 |
$categories = get_the_terms( $post_id, 'category' ); |
|
9 | 79 |
if ( ! $categories || is_wp_error( $categories ) ) { |
0 | 80 |
$categories = array(); |
9 | 81 |
} |
0 | 82 |
|
83 |
$categories = array_values( $categories ); |
|
84 |
||
85 |
foreach ( array_keys( $categories ) as $key ) { |
|
9 | 86 |
_make_cat_compat( $categories[ $key ] ); |
0 | 87 |
} |
88 |
||
5 | 89 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
* Filters the array of categories to return for a post. |
5 | 91 |
* |
92 |
* @since 3.1.0 |
|
16 | 93 |
* @since 4.4.0 Added `$post_id` parameter. |
5 | 94 |
* |
9 | 95 |
* @param WP_Term[] $categories An array of categories to return for the post. |
16 | 96 |
* @param int|false $post_id ID of the post. |
5 | 97 |
*/ |
16 | 98 |
return apply_filters( 'get_the_categories', $categories, $post_id ); |
0 | 99 |
} |
100 |
||
101 |
/** |
|
16 | 102 |
* Retrieves category name based on category ID. |
0 | 103 |
* |
104 |
* @since 0.71 |
|
105 |
* |
|
19 | 106 |
* @param int $cat_id Category ID. |
0 | 107 |
* @return string|WP_Error Category name on success, WP_Error on failure. |
108 |
*/ |
|
19 | 109 |
function get_the_category_by_ID( $cat_id ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid |
110 |
$cat_id = (int) $cat_id; |
|
111 |
$category = get_term( $cat_id ); |
|
5 | 112 |
|
9 | 113 |
if ( is_wp_error( $category ) ) { |
0 | 114 |
return $category; |
9 | 115 |
} |
5 | 116 |
|
117 |
return ( $category ) ? $category->name : ''; |
|
0 | 118 |
} |
119 |
||
120 |
/** |
|
16 | 121 |
* Retrieves category list for a post in either HTML list or custom format. |
122 |
* |
|
123 |
* Generally used for quick, delimited (e.g. comma-separated) lists of categories, |
|
124 |
* as part of a post entry meta. |
|
125 |
* |
|
126 |
* For a more powerful, list-based function, see wp_list_categories(). |
|
0 | 127 |
* |
128 |
* @since 1.5.1 |
|
129 |
* |
|
16 | 130 |
* @see wp_list_categories() |
131 |
* |
|
132 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
133 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
134 |
* @param string $separator Optional. Separator between the categories. By default, the links are placed |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
135 |
* in an unordered list. An empty string will result in the default behavior. |
16 | 136 |
* @param string $parents Optional. How to display the parents. |
137 |
* @param int $post_id Optional. Post ID to retrieve categories. |
|
138 |
* @return string Category list for a post. |
|
0 | 139 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
140 |
function get_the_category_list( $separator = '', $parents = '', $post_id = false ) { |
0 | 141 |
global $wp_rewrite; |
16 | 142 |
|
5 | 143 |
if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) ) { |
144 |
/** This filter is documented in wp-includes/category-template.php */ |
|
0 | 145 |
return apply_filters( 'the_category', '', $separator, $parents ); |
5 | 146 |
} |
0 | 147 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
148 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
149 |
* Filters the categories before building the category list. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
150 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
151 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
* |
9 | 153 |
* @param WP_Term[] $categories An array of the post's categories. |
16 | 154 |
* @param int|bool $post_id ID of the post we're retrieving categories for. |
155 |
* When `false`, we assume the current post in the loop. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
156 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
157 |
$categories = apply_filters( 'the_category_list', get_the_category( $post_id ), $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
158 |
|
5 | 159 |
if ( empty( $categories ) ) { |
160 |
/** This filter is documented in wp-includes/category-template.php */ |
|
0 | 161 |
return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents ); |
5 | 162 |
} |
0 | 163 |
|
164 |
$rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"'; |
|
165 |
||
166 |
$thelist = ''; |
|
16 | 167 |
if ( '' === $separator ) { |
0 | 168 |
$thelist .= '<ul class="post-categories">'; |
169 |
foreach ( $categories as $category ) { |
|
170 |
$thelist .= "\n\t<li>"; |
|
171 |
switch ( strtolower( $parents ) ) { |
|
172 |
case 'multiple': |
|
9 | 173 |
if ( $category->parent ) { |
0 | 174 |
$thelist .= get_category_parents( $category->parent, true, $separator ); |
9 | 175 |
} |
176 |
$thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name . '</a></li>'; |
|
0 | 177 |
break; |
178 |
case 'single': |
|
5 | 179 |
$thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>'; |
9 | 180 |
if ( $category->parent ) { |
0 | 181 |
$thelist .= get_category_parents( $category->parent, false, $separator ); |
9 | 182 |
} |
183 |
$thelist .= $category->name . '</a></li>'; |
|
0 | 184 |
break; |
185 |
case '': |
|
186 |
default: |
|
9 | 187 |
$thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name . '</a></li>'; |
0 | 188 |
} |
189 |
} |
|
190 |
$thelist .= '</ul>'; |
|
191 |
} else { |
|
192 |
$i = 0; |
|
193 |
foreach ( $categories as $category ) { |
|
9 | 194 |
if ( 0 < $i ) { |
0 | 195 |
$thelist .= $separator; |
9 | 196 |
} |
0 | 197 |
switch ( strtolower( $parents ) ) { |
198 |
case 'multiple': |
|
9 | 199 |
if ( $category->parent ) { |
0 | 200 |
$thelist .= get_category_parents( $category->parent, true, $separator ); |
9 | 201 |
} |
202 |
$thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name . '</a>'; |
|
0 | 203 |
break; |
204 |
case 'single': |
|
5 | 205 |
$thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>'; |
9 | 206 |
if ( $category->parent ) { |
0 | 207 |
$thelist .= get_category_parents( $category->parent, false, $separator ); |
9 | 208 |
} |
0 | 209 |
$thelist .= "$category->name</a>"; |
210 |
break; |
|
211 |
case '': |
|
212 |
default: |
|
9 | 213 |
$thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name . '</a>'; |
0 | 214 |
} |
215 |
++$i; |
|
216 |
} |
|
217 |
} |
|
5 | 218 |
|
219 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
220 |
* Filters the category or list of categories. |
5 | 221 |
* |
222 |
* @since 1.2.0 |
|
223 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
224 |
* @param string $thelist List of categories for the current post. |
5 | 225 |
* @param string $separator Separator used between the categories. |
226 |
* @param string $parents How to display the category parents. Accepts 'multiple', |
|
227 |
* 'single', or empty. |
|
228 |
*/ |
|
0 | 229 |
return apply_filters( 'the_category', $thelist, $separator, $parents ); |
230 |
} |
|
231 |
||
232 |
/** |
|
9 | 233 |
* Checks if the current post is within any of the given categories. |
0 | 234 |
* |
235 |
* The given categories are checked against the post's categories' term_ids, names and slugs. |
|
236 |
* Categories given as integers will only be checked against the post's categories' term_ids. |
|
237 |
* |
|
238 |
* Prior to v2.5 of WordPress, category names were not supported. |
|
239 |
* Prior to v2.7, category slugs were not supported. |
|
240 |
* Prior to v2.7, only one category could be compared: in_category( $single_category ). |
|
241 |
* Prior to v2.7, this function could only be used in the WordPress Loop. |
|
242 |
* As of 2.7, the function can be used anywhere if it is provided a post ID or post object. |
|
243 |
* |
|
9 | 244 |
* For more information on this and similar theme functions, check out |
245 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
246 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
247 |
* |
|
0 | 248 |
* @since 1.2.0 |
16 | 249 |
* @since 2.7.0 The `$post` parameter was added. |
0 | 250 |
* |
18 | 251 |
* @param int|string|int[]|string[] $category Category ID, name, slug, or array of such |
252 |
* to check against. |
|
253 |
* @param int|object $post Optional. Post to check instead of the current post. |
|
0 | 254 |
* @return bool True if the current post is in any of the given categories. |
255 |
*/ |
|
256 |
function in_category( $category, $post = null ) { |
|
9 | 257 |
if ( empty( $category ) ) { |
0 | 258 |
return false; |
9 | 259 |
} |
0 | 260 |
|
261 |
return has_category( $category, $post ); |
|
262 |
} |
|
263 |
||
264 |
/** |
|
16 | 265 |
* Displays category list for a post in either HTML list or custom format. |
0 | 266 |
* |
267 |
* @since 0.71 |
|
268 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
269 |
* @param string $separator Optional. Separator between the categories. By default, the links are placed |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
270 |
* in an unordered list. An empty string will result in the default behavior. |
16 | 271 |
* @param string $parents Optional. How to display the parents. |
272 |
* @param int $post_id Optional. Post ID to retrieve categories. |
|
0 | 273 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
274 |
function the_category( $separator = '', $parents = '', $post_id = false ) { |
0 | 275 |
echo get_the_category_list( $separator, $parents, $post_id ); |
276 |
} |
|
277 |
||
278 |
/** |
|
16 | 279 |
* Retrieves category description. |
0 | 280 |
* |
281 |
* @since 1.0.0 |
|
282 |
* |
|
16 | 283 |
* @param int $category Optional. Category ID. Defaults to the current category ID. |
284 |
* @return string Category description, if available. |
|
0 | 285 |
*/ |
286 |
function category_description( $category = 0 ) { |
|
9 | 287 |
return term_description( $category ); |
0 | 288 |
} |
289 |
||
290 |
/** |
|
16 | 291 |
* Displays or retrieves the HTML dropdown list of categories. |
0 | 292 |
* |
293 |
* The 'hierarchical' argument, which is disabled by default, will override the |
|
294 |
* depth argument, unless it is true. When the argument is false, it will |
|
295 |
* display all of the categories. When it is enabled it will use the value in |
|
296 |
* the 'depth' argument. |
|
297 |
* |
|
298 |
* @since 2.1.0 |
|
5 | 299 |
* @since 4.2.0 Introduced the `value_field` argument. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
300 |
* @since 4.6.0 Introduced the `required` argument. |
0 | 301 |
* |
16 | 302 |
* @param array|string $args { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
303 |
* Optional. Array or string of arguments to generate a categories drop-down element. See WP_Term_Query::__construct() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
304 |
* for information on additional accepted arguments. |
5 | 305 |
* |
306 |
* @type string $show_option_all Text to display for showing all categories. Default empty. |
|
307 |
* @type string $show_option_none Text to display for showing no categories. Default empty. |
|
308 |
* @type string $option_none_value Value to use when no category is selected. Default empty. |
|
309 |
* @type string $orderby Which column to use for ordering categories. See get_terms() for a list |
|
310 |
* of accepted values. Default 'id' (term_id). |
|
311 |
* @type bool $pad_counts See get_terms() for an argument description. Default false. |
|
312 |
* @type bool|int $show_count Whether to include post counts. Accepts 0, 1, or their bool equivalents. |
|
313 |
* Default 0. |
|
314 |
* @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1, or their |
|
315 |
* bool equivalents. Default 1. |
|
316 |
* @type bool|int $hierarchical Whether to traverse the taxonomy hierarchy. Accepts 0, 1, or their bool |
|
317 |
* equivalents. Default 0. |
|
318 |
* @type int $depth Maximum depth. Default 0. |
|
319 |
* @type int $tab_index Tab index for the select element. Default 0 (no tabindex). |
|
320 |
* @type string $name Value for the 'name' attribute of the select element. Default 'cat'. |
|
321 |
* @type string $id Value for the 'id' attribute of the select element. Defaults to the value |
|
322 |
* of `$name`. |
|
323 |
* @type string $class Value for the 'class' attribute of the select element. Default 'postform'. |
|
324 |
* @type int|string $selected Value of the option that should be selected. Default 0. |
|
325 |
* @type string $value_field Term field that should be used to populate the 'value' attribute |
|
326 |
* of the option elements. Accepts any valid term field: 'term_id', 'name', |
|
327 |
* 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description', |
|
328 |
* 'parent', 'count'. Default 'term_id'. |
|
18 | 329 |
* @type string|array $taxonomy Name of the taxonomy or taxonomies to retrieve. Default 'category'. |
5 | 330 |
* @type bool $hide_if_empty True to skip generating markup if no categories are found. |
331 |
* Default false (create select element even if no categories are found). |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
332 |
* @type bool $required Whether the `<select>` element should have the HTML5 'required' attribute. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
333 |
* Default false. |
19 | 334 |
* @type Walker $walker Walker object to use to build the output. Default empty which results in a |
335 |
* Walker_CategoryDropdown instance being used. |
|
5 | 336 |
* } |
16 | 337 |
* @return string HTML dropdown list of categories. |
0 | 338 |
*/ |
339 |
function wp_dropdown_categories( $args = '' ) { |
|
340 |
$defaults = array( |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
341 |
'show_option_all' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
342 |
'show_option_none' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
343 |
'orderby' => 'id', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
344 |
'order' => 'ASC', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
345 |
'show_count' => 0, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
346 |
'hide_empty' => 1, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
347 |
'child_of' => 0, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
348 |
'exclude' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
349 |
'echo' => 1, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
350 |
'selected' => 0, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
351 |
'hierarchical' => 0, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
352 |
'name' => 'cat', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
353 |
'id' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
354 |
'class' => 'postform', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
355 |
'depth' => 0, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
356 |
'tab_index' => 0, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
357 |
'taxonomy' => 'category', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
358 |
'hide_if_empty' => false, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
359 |
'option_none_value' => -1, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
360 |
'value_field' => 'term_id', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
361 |
'required' => false, |
0 | 362 |
); |
363 |
||
364 |
$defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0; |
|
365 |
||
366 |
// Back compat. |
|
16 | 367 |
if ( isset( $args['type'] ) && 'link' === $args['type'] ) { |
9 | 368 |
_deprecated_argument( |
369 |
__FUNCTION__, |
|
370 |
'3.0.0', |
|
371 |
sprintf( |
|
16 | 372 |
/* translators: 1: "type => link", 2: "taxonomy => link_category" */ |
9 | 373 |
__( '%1$s is deprecated. Use %2$s instead.' ), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
374 |
'<code>type => link</code>', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
375 |
'<code>taxonomy => link_category</code>' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
376 |
) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
377 |
); |
0 | 378 |
$args['taxonomy'] = 'link_category'; |
379 |
} |
|
380 |
||
16 | 381 |
// Parse incoming $args into an array and merge it with $defaults. |
382 |
$parsed_args = wp_parse_args( $args, $defaults ); |
|
0 | 383 |
|
16 | 384 |
$option_none_value = $parsed_args['option_none_value']; |
385 |
||
386 |
if ( ! isset( $parsed_args['pad_counts'] ) && $parsed_args['show_count'] && $parsed_args['hierarchical'] ) { |
|
387 |
$parsed_args['pad_counts'] = true; |
|
0 | 388 |
} |
389 |
||
16 | 390 |
$tab_index = $parsed_args['tab_index']; |
0 | 391 |
|
392 |
$tab_index_attribute = ''; |
|
5 | 393 |
if ( (int) $tab_index > 0 ) { |
0 | 394 |
$tab_index_attribute = " tabindex=\"$tab_index\""; |
5 | 395 |
} |
0 | 396 |
|
5 | 397 |
// Avoid clashes with the 'name' param of get_terms(). |
16 | 398 |
$get_terms_args = $parsed_args; |
5 | 399 |
unset( $get_terms_args['name'] ); |
16 | 400 |
$categories = get_terms( $get_terms_args ); |
5 | 401 |
|
16 | 402 |
$name = esc_attr( $parsed_args['name'] ); |
403 |
$class = esc_attr( $parsed_args['class'] ); |
|
404 |
$id = $parsed_args['id'] ? esc_attr( $parsed_args['id'] ) : $name; |
|
405 |
$required = $parsed_args['required'] ? 'required' : ''; |
|
0 | 406 |
|
16 | 407 |
if ( ! $parsed_args['hide_if_empty'] || ! empty( $categories ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
408 |
$output = "<select $required name='$name' id='$id' class='$class' $tab_index_attribute>\n"; |
5 | 409 |
} else { |
0 | 410 |
$output = ''; |
5 | 411 |
} |
16 | 412 |
if ( empty( $categories ) && ! $parsed_args['hide_if_empty'] && ! empty( $parsed_args['show_option_none'] ) ) { |
0 | 413 |
|
5 | 414 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
415 |
* Filters a taxonomy drop-down display element. |
5 | 416 |
* |
417 |
* A variety of taxonomy drop-down display elements can be modified |
|
418 |
* just prior to display via this filter. Filterable arguments include |
|
419 |
* 'show_option_none', 'show_option_all', and various forms of the |
|
420 |
* term name. |
|
421 |
* |
|
422 |
* @since 1.2.0 |
|
423 |
* |
|
424 |
* @see wp_dropdown_categories() |
|
425 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
426 |
* @param string $element Category name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
427 |
* @param WP_Term|null $category The category object, or null if there's no corresponding category. |
5 | 428 |
*/ |
16 | 429 |
$show_option_none = apply_filters( 'list_cats', $parsed_args['show_option_none'], null ); |
9 | 430 |
$output .= "\t<option value='" . esc_attr( $option_none_value ) . "' selected='selected'>$show_option_none</option>\n"; |
0 | 431 |
} |
432 |
||
433 |
if ( ! empty( $categories ) ) { |
|
434 |
||
16 | 435 |
if ( $parsed_args['show_option_all'] ) { |
5 | 436 |
|
437 |
/** This filter is documented in wp-includes/category-template.php */ |
|
16 | 438 |
$show_option_all = apply_filters( 'list_cats', $parsed_args['show_option_all'], null ); |
18 | 439 |
$selected = ( '0' === (string) $parsed_args['selected'] ) ? " selected='selected'" : ''; |
9 | 440 |
$output .= "\t<option value='0'$selected>$show_option_all</option>\n"; |
0 | 441 |
} |
442 |
||
16 | 443 |
if ( $parsed_args['show_option_none'] ) { |
5 | 444 |
|
445 |
/** This filter is documented in wp-includes/category-template.php */ |
|
16 | 446 |
$show_option_none = apply_filters( 'list_cats', $parsed_args['show_option_none'], null ); |
447 |
$selected = selected( $option_none_value, $parsed_args['selected'], false ); |
|
9 | 448 |
$output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$selected>$show_option_none</option>\n"; |
0 | 449 |
} |
450 |
||
16 | 451 |
if ( $parsed_args['hierarchical'] ) { |
452 |
$depth = $parsed_args['depth']; // Walk the full depth. |
|
5 | 453 |
} else { |
0 | 454 |
$depth = -1; // Flat. |
5 | 455 |
} |
16 | 456 |
$output .= walk_category_dropdown_tree( $categories, $depth, $parsed_args ); |
0 | 457 |
} |
458 |
||
16 | 459 |
if ( ! $parsed_args['hide_if_empty'] || ! empty( $categories ) ) { |
0 | 460 |
$output .= "</select>\n"; |
5 | 461 |
} |
16 | 462 |
|
5 | 463 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
464 |
* Filters the taxonomy drop-down output. |
5 | 465 |
* |
466 |
* @since 2.1.0 |
|
467 |
* |
|
16 | 468 |
* @param string $output HTML output. |
469 |
* @param array $parsed_args Arguments used to build the drop-down. |
|
5 | 470 |
*/ |
16 | 471 |
$output = apply_filters( 'wp_dropdown_cats', $output, $parsed_args ); |
0 | 472 |
|
16 | 473 |
if ( $parsed_args['echo'] ) { |
0 | 474 |
echo $output; |
5 | 475 |
} |
16 | 476 |
|
0 | 477 |
return $output; |
478 |
} |
|
479 |
||
480 |
/** |
|
16 | 481 |
* Displays or retrieves the HTML list of categories. |
0 | 482 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
483 |
* @since 2.1.0 |
16 | 484 |
* @since 4.4.0 Introduced the `hide_title_if_empty` and `separator` arguments. |
485 |
* @since 4.4.0 The `current_category` argument was modified to optionally accept an array of values. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
486 |
* |
16 | 487 |
* @param array|string $args { |
488 |
* Array of optional arguments. See get_categories(), get_terms(), and WP_Term_Query::__construct() |
|
489 |
* for information on additional accepted arguments. |
|
0 | 490 |
* |
18 | 491 |
* @type int|int[] $current_category ID of category, or array of IDs of categories, that should get the |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
492 |
* 'current-cat' class. Default 0. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
493 |
* @type int $depth Category depth. Used for tab indentation. Default 0. |
16 | 494 |
* @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1, or their |
495 |
* bool equivalents. Default 1. |
|
18 | 496 |
* @type int[]|string $exclude Array or comma/space-separated string of term IDs to exclude. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
497 |
* If `$hierarchical` is true, descendants of `$exclude` terms will also |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
498 |
* be excluded; see `$exclude_tree`. See get_terms(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
499 |
* Default empty string. |
18 | 500 |
* @type int[]|string $exclude_tree Array or comma/space-separated string of term IDs to exclude, along |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
501 |
* with their descendants. See get_terms(). Default empty string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
502 |
* @type string $feed Text to use for the feed link. Default 'Feed for all posts filed |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
503 |
* under [cat name]'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
504 |
* @type string $feed_image URL of an image to use for the feed link. Default empty string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
505 |
* @type string $feed_type Feed type. Used to build feed link. See get_term_feed_link(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
506 |
* Default empty string (default feed). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
507 |
* @type bool $hide_title_if_empty Whether to hide the `$title_li` element if there are no terms in |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
508 |
* the list. Default false (title will always be shown). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
509 |
* @type string $separator Separator between links. Default '<br />'. |
16 | 510 |
* @type bool|int $show_count Whether to include post counts. Accepts 0, 1, or their bool equivalents. |
511 |
* Default 0. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
512 |
* @type string $show_option_all Text to display for showing all categories. Default empty string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
513 |
* @type string $show_option_none Text to display for the 'no categories' option. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
514 |
* Default 'No categories'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
515 |
* @type string $style The style used to display the categories list. If 'list', categories |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
516 |
* will be output as an unordered list. If left empty or another value, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
517 |
* categories will be output separated by `<br>` tags. Default 'list'. |
18 | 518 |
* @type string $taxonomy Name of the taxonomy to retrieve. Default 'category'. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
519 |
* @type string $title_li Text to use for the list title `<li>` element. Pass an empty string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
520 |
* to disable. Default 'Categories'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
521 |
* @type bool|int $use_desc_for_title Whether to use the category description as the title attribute. |
16 | 522 |
* Accepts 0, 1, or their bool equivalents. Default 1. |
19 | 523 |
* @type Walker $walker Walker object to use to build the output. Default empty which results |
524 |
* in a Walker_Category instance being used. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
525 |
* } |
16 | 526 |
* @return void|string|false Void if 'echo' argument is true, HTML list of categories if 'echo' is false. |
527 |
* False if the taxonomy does not exist. |
|
0 | 528 |
*/ |
529 |
function wp_list_categories( $args = '' ) { |
|
530 |
$defaults = array( |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
531 |
'child_of' => 0, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
532 |
'current_category' => 0, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
533 |
'depth' => 0, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
534 |
'echo' => 1, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
535 |
'exclude' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
536 |
'exclude_tree' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
537 |
'feed' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
538 |
'feed_image' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
539 |
'feed_type' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
540 |
'hide_empty' => 1, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
541 |
'hide_title_if_empty' => false, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
542 |
'hierarchical' => true, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
543 |
'order' => 'ASC', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
544 |
'orderby' => 'name', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
545 |
'separator' => '<br />', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
546 |
'show_count' => 0, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
547 |
'show_option_all' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
548 |
'show_option_none' => __( 'No categories' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
549 |
'style' => 'list', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
550 |
'taxonomy' => 'category', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
551 |
'title_li' => __( 'Categories' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
552 |
'use_desc_for_title' => 1, |
0 | 553 |
); |
554 |
||
16 | 555 |
$parsed_args = wp_parse_args( $args, $defaults ); |
0 | 556 |
|
16 | 557 |
if ( ! isset( $parsed_args['pad_counts'] ) && $parsed_args['show_count'] && $parsed_args['hierarchical'] ) { |
558 |
$parsed_args['pad_counts'] = true; |
|
9 | 559 |
} |
0 | 560 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
561 |
// Descendants of exclusions should be excluded too. |
16 | 562 |
if ( true == $parsed_args['hierarchical'] ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
563 |
$exclude_tree = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
564 |
|
16 | 565 |
if ( $parsed_args['exclude_tree'] ) { |
566 |
$exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $parsed_args['exclude_tree'] ) ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
567 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
568 |
|
16 | 569 |
if ( $parsed_args['exclude'] ) { |
570 |
$exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $parsed_args['exclude'] ) ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
571 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
572 |
|
16 | 573 |
$parsed_args['exclude_tree'] = $exclude_tree; |
574 |
$parsed_args['exclude'] = ''; |
|
0 | 575 |
} |
576 |
||
16 | 577 |
if ( ! isset( $parsed_args['class'] ) ) { |
578 |
$parsed_args['class'] = ( 'category' === $parsed_args['taxonomy'] ) ? 'categories' : $parsed_args['taxonomy']; |
|
9 | 579 |
} |
0 | 580 |
|
16 | 581 |
if ( ! taxonomy_exists( $parsed_args['taxonomy'] ) ) { |
5 | 582 |
return false; |
583 |
} |
|
0 | 584 |
|
16 | 585 |
$show_option_all = $parsed_args['show_option_all']; |
586 |
$show_option_none = $parsed_args['show_option_none']; |
|
0 | 587 |
|
16 | 588 |
$categories = get_categories( $parsed_args ); |
0 | 589 |
|
590 |
$output = ''; |
|
16 | 591 |
|
592 |
if ( $parsed_args['title_li'] && 'list' === $parsed_args['style'] |
|
593 |
&& ( ! empty( $categories ) || ! $parsed_args['hide_title_if_empty'] ) |
|
594 |
) { |
|
595 |
$output = '<li class="' . esc_attr( $parsed_args['class'] ) . '">' . $parsed_args['title_li'] . '<ul>'; |
|
5 | 596 |
} |
16 | 597 |
|
0 | 598 |
if ( empty( $categories ) ) { |
599 |
if ( ! empty( $show_option_none ) ) { |
|
16 | 600 |
if ( 'list' === $parsed_args['style'] ) { |
5 | 601 |
$output .= '<li class="cat-item-none">' . $show_option_none . '</li>'; |
602 |
} else { |
|
0 | 603 |
$output .= $show_option_none; |
5 | 604 |
} |
0 | 605 |
} |
606 |
} else { |
|
607 |
if ( ! empty( $show_option_all ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
608 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
609 |
$posts_page = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
610 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
611 |
// For taxonomies that belong only to custom post types, point to a valid archive. |
16 | 612 |
$taxonomy_object = get_taxonomy( $parsed_args['taxonomy'] ); |
613 |
if ( ! in_array( 'post', $taxonomy_object->object_type, true ) && ! in_array( 'page', $taxonomy_object->object_type, true ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
614 |
foreach ( $taxonomy_object->object_type as $object_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
615 |
$_object_type = get_post_type_object( $object_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
616 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
617 |
// Grab the first one. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
618 |
if ( ! empty( $_object_type->has_archive ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
619 |
$posts_page = get_post_type_archive_link( $object_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
620 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
621 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
622 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
623 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
624 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
625 |
// Fallback for the 'All' link is the posts page. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
626 |
if ( ! $posts_page ) { |
16 | 627 |
if ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
628 |
$posts_page = get_permalink( get_option( 'page_for_posts' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
629 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
630 |
$posts_page = home_url( '/' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
631 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
632 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
633 |
|
0 | 634 |
$posts_page = esc_url( $posts_page ); |
16 | 635 |
if ( 'list' === $parsed_args['style'] ) { |
5 | 636 |
$output .= "<li class='cat-item-all'><a href='$posts_page'>$show_option_all</a></li>"; |
637 |
} else { |
|
0 | 638 |
$output .= "<a href='$posts_page'>$show_option_all</a>"; |
5 | 639 |
} |
0 | 640 |
} |
641 |
||
16 | 642 |
if ( empty( $parsed_args['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) { |
0 | 643 |
$current_term_object = get_queried_object(); |
16 | 644 |
if ( $current_term_object && $parsed_args['taxonomy'] === $current_term_object->taxonomy ) { |
645 |
$parsed_args['current_category'] = get_queried_object_id(); |
|
5 | 646 |
} |
0 | 647 |
} |
648 |
||
16 | 649 |
if ( $parsed_args['hierarchical'] ) { |
650 |
$depth = $parsed_args['depth']; |
|
5 | 651 |
} else { |
0 | 652 |
$depth = -1; // Flat. |
5 | 653 |
} |
16 | 654 |
$output .= walk_category_tree( $categories, $depth, $parsed_args ); |
0 | 655 |
} |
656 |
||
16 | 657 |
if ( $parsed_args['title_li'] && 'list' === $parsed_args['style'] |
658 |
&& ( ! empty( $categories ) || ! $parsed_args['hide_title_if_empty'] ) |
|
659 |
) { |
|
0 | 660 |
$output .= '</ul></li>'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
661 |
} |
0 | 662 |
|
5 | 663 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
664 |
* Filters the HTML output of a taxonomy list. |
5 | 665 |
* |
666 |
* @since 2.1.0 |
|
667 |
* |
|
19 | 668 |
* @param string $output HTML output. |
669 |
* @param array|string $args An array or query string of taxonomy-listing arguments. See |
|
670 |
* wp_list_categories() for information on accepted arguments. |
|
5 | 671 |
*/ |
672 |
$html = apply_filters( 'wp_list_categories', $output, $args ); |
|
0 | 673 |
|
16 | 674 |
if ( $parsed_args['echo'] ) { |
5 | 675 |
echo $html; |
676 |
} else { |
|
677 |
return $html; |
|
678 |
} |
|
0 | 679 |
} |
680 |
||
681 |
/** |
|
9 | 682 |
* Displays a tag cloud. |
0 | 683 |
* |
16 | 684 |
* Outputs a list of tags in what is called a 'tag cloud', where the size of each tag |
685 |
* is determined by how many times that particular tag has been assigned to posts. |
|
686 |
* |
|
0 | 687 |
* @since 2.3.0 |
16 | 688 |
* @since 2.8.0 Added the `taxonomy` argument. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
689 |
* @since 4.8.0 Added the `show_count` argument. |
0 | 690 |
* |
9 | 691 |
* @param array|string $args { |
692 |
* Optional. Array or string of arguments for displaying a tag cloud. See wp_generate_tag_cloud() |
|
693 |
* and get_terms() for the full lists of arguments that can be passed in `$args`. |
|
694 |
* |
|
695 |
* @type int $number The number of tags to display. Accepts any positive integer |
|
18 | 696 |
* or zero to return all. Default 45. |
9 | 697 |
* @type string $link Whether to display term editing links or term permalinks. |
698 |
* Accepts 'edit' and 'view'. Default 'view'. |
|
699 |
* @type string $post_type The post type. Used to highlight the proper post type menu |
|
700 |
* on the linked edit page. Defaults to the first post type |
|
701 |
* associated with the taxonomy. |
|
702 |
* @type bool $echo Whether or not to echo the return value. Default true. |
|
703 |
* } |
|
18 | 704 |
* @return void|string|string[] Void if 'echo' argument is true, or on failure. Otherwise, tag cloud |
705 |
* as a string or an array, depending on 'format' argument. |
|
0 | 706 |
*/ |
707 |
function wp_tag_cloud( $args = '' ) { |
|
708 |
$defaults = array( |
|
9 | 709 |
'smallest' => 8, |
710 |
'largest' => 22, |
|
711 |
'unit' => 'pt', |
|
712 |
'number' => 45, |
|
713 |
'format' => 'flat', |
|
714 |
'separator' => "\n", |
|
715 |
'orderby' => 'name', |
|
716 |
'order' => 'ASC', |
|
717 |
'exclude' => '', |
|
718 |
'include' => '', |
|
719 |
'link' => 'view', |
|
720 |
'taxonomy' => 'post_tag', |
|
721 |
'post_type' => '', |
|
722 |
'echo' => true, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
723 |
'show_count' => 0, |
0 | 724 |
); |
16 | 725 |
|
726 |
$args = wp_parse_args( $args, $defaults ); |
|
0 | 727 |
|
9 | 728 |
$tags = get_terms( |
729 |
array_merge( |
|
730 |
$args, |
|
731 |
array( |
|
732 |
'orderby' => 'count', |
|
733 |
'order' => 'DESC', |
|
734 |
) |
|
735 |
) |
|
16 | 736 |
); // Always query top tags. |
0 | 737 |
|
9 | 738 |
if ( empty( $tags ) || is_wp_error( $tags ) ) { |
0 | 739 |
return; |
9 | 740 |
} |
0 | 741 |
|
742 |
foreach ( $tags as $key => $tag ) { |
|
16 | 743 |
if ( 'edit' === $args['link'] ) { |
19 | 744 |
$link = get_edit_term_link( $tag, $tag->taxonomy, $args['post_type'] ); |
9 | 745 |
} else { |
19 | 746 |
$link = get_term_link( $tag, $tag->taxonomy ); |
9 | 747 |
} |
16 | 748 |
|
9 | 749 |
if ( is_wp_error( $link ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
750 |
return; |
9 | 751 |
} |
0 | 752 |
|
753 |
$tags[ $key ]->link = $link; |
|
9 | 754 |
$tags[ $key ]->id = $tag->term_id; |
0 | 755 |
} |
756 |
||
16 | 757 |
// Here's where those top tags get sorted according to $args. |
758 |
$return = wp_generate_tag_cloud( $tags, $args ); |
|
0 | 759 |
|
5 | 760 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
761 |
* Filters the tag cloud output. |
5 | 762 |
* |
763 |
* @since 2.3.0 |
|
764 |
* |
|
18 | 765 |
* @param string|string[] $return Tag cloud as a string or an array, depending on 'format' argument. |
766 |
* @param array $args An array of tag cloud arguments. See wp_tag_cloud() |
|
767 |
* for information on accepted arguments. |
|
5 | 768 |
*/ |
0 | 769 |
$return = apply_filters( 'wp_tag_cloud', $return, $args ); |
770 |
||
16 | 771 |
if ( 'array' === $args['format'] || empty( $args['echo'] ) ) { |
0 | 772 |
return $return; |
9 | 773 |
} |
0 | 774 |
|
775 |
echo $return; |
|
776 |
} |
|
777 |
||
778 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
779 |
* Default topic count scaling for tag links. |
0 | 780 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
781 |
* @since 2.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
782 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
783 |
* @param int $count Number of posts with that tag. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
784 |
* @return int Scaled count. |
0 | 785 |
*/ |
786 |
function default_topic_count_scale( $count ) { |
|
9 | 787 |
return round( log10( $count + 1 ) * 100 ); |
0 | 788 |
} |
789 |
||
790 |
/** |
|
791 |
* Generates a tag cloud (heatmap) from provided data. |
|
792 |
* |
|
793 |
* @todo Complete functionality. |
|
794 |
* @since 2.3.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
795 |
* @since 4.8.0 Added the `show_count` argument. |
0 | 796 |
* |
9 | 797 |
* @param WP_Term[] $tags Array of WP_Term objects to generate the tag cloud for. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
798 |
* @param string|array $args { |
9 | 799 |
* Optional. Array or string of arguments for generating a tag cloud. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
800 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
801 |
* @type int $smallest Smallest font size used to display tags. Paired |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
802 |
* with the value of `$unit`, to determine CSS text |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
803 |
* size unit. Default 8 (pt). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
804 |
* @type int $largest Largest font size used to display tags. Paired |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
805 |
* with the value of `$unit`, to determine CSS text |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
806 |
* size unit. Default 22 (pt). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
807 |
* @type string $unit CSS text size unit to use with the `$smallest` |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
808 |
* and `$largest` values. Accepts any valid CSS text |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
809 |
* size unit. Default 'pt'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
810 |
* @type int $number The number of tags to return. Accepts any |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
811 |
* positive integer or zero to return all. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
812 |
* Default 0. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
813 |
* @type string $format Format to display the tag cloud in. Accepts 'flat' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
814 |
* (tags separated with spaces), 'list' (tags displayed |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
815 |
* in an unordered list), or 'array' (returns an array). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
816 |
* Default 'flat'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
817 |
* @type string $separator HTML or text to separate the tags. Default "\n" (newline). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
818 |
* @type string $orderby Value to order tags by. Accepts 'name' or 'count'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
819 |
* Default 'name'. The {@see 'tag_cloud_sort'} filter |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
820 |
* can also affect how tags are sorted. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
821 |
* @type string $order How to order the tags. Accepts 'ASC' (ascending), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
822 |
* 'DESC' (descending), or 'RAND' (random). Default 'ASC'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
823 |
* @type int|bool $filter Whether to enable filtering of the final output |
18 | 824 |
* via {@see 'wp_generate_tag_cloud'}. Default 1. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
825 |
* @type string $topic_count_text Nooped plural text from _n_noop() to supply to |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
826 |
* tag counts. Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
827 |
* @type callable $topic_count_text_callback Callback used to generate nooped plural text for |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
828 |
* tag counts based on the count. Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
829 |
* @type callable $topic_count_scale_callback Callback used to determine the tag count scaling |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
830 |
* value. Default default_topic_count_scale(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
831 |
* @type bool|int $show_count Whether to display the tag counts. Default 0. Accepts |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
832 |
* 0, 1, or their bool equivalents. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
833 |
* } |
18 | 834 |
* @return string|string[] Tag cloud as a string or an array, depending on 'format' argument. |
0 | 835 |
*/ |
836 |
function wp_generate_tag_cloud( $tags, $args = '' ) { |
|
837 |
$defaults = array( |
|
9 | 838 |
'smallest' => 8, |
839 |
'largest' => 22, |
|
840 |
'unit' => 'pt', |
|
841 |
'number' => 0, |
|
842 |
'format' => 'flat', |
|
843 |
'separator' => "\n", |
|
844 |
'orderby' => 'name', |
|
845 |
'order' => 'ASC', |
|
846 |
'topic_count_text' => null, |
|
847 |
'topic_count_text_callback' => null, |
|
848 |
'topic_count_scale_callback' => 'default_topic_count_scale', |
|
849 |
'filter' => 1, |
|
850 |
'show_count' => 0, |
|
0 | 851 |
); |
852 |
||
5 | 853 |
$args = wp_parse_args( $args, $defaults ); |
854 |
||
855 |
$return = ( 'array' === $args['format'] ) ? array() : ''; |
|
856 |
||
857 |
if ( empty( $tags ) ) { |
|
858 |
return $return; |
|
859 |
} |
|
860 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
861 |
// Juggle topic counts. |
5 | 862 |
if ( isset( $args['topic_count_text'] ) ) { |
863 |
// First look for nooped plural support via topic_count_text. |
|
864 |
$translate_nooped_plural = $args['topic_count_text']; |
|
865 |
} elseif ( ! empty( $args['topic_count_text_callback'] ) ) { |
|
866 |
// Look for the alternative callback style. Ignore the previous default. |
|
16 | 867 |
if ( 'default_topic_count_text' === $args['topic_count_text_callback'] ) { |
868 |
/* translators: %s: Number of items (tags). */ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
869 |
$translate_nooped_plural = _n_noop( '%s item', '%s items' ); |
5 | 870 |
} else { |
871 |
$translate_nooped_plural = false; |
|
872 |
} |
|
873 |
} elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) { |
|
874 |
// If no callback exists, look for the old-style single_text and multiple_text arguments. |
|
9 | 875 |
// phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingle,WordPress.WP.I18n.NonSingularStringLiteralPlural |
5 | 876 |
$translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] ); |
877 |
} else { |
|
878 |
// This is the default for when no callback, plural, or argument is passed in. |
|
16 | 879 |
/* translators: %s: Number of items (tags). */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
880 |
$translate_nooped_plural = _n_noop( '%s item', '%s items' ); |
0 | 881 |
} |
882 |
||
5 | 883 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
884 |
* Filters how the items in a tag cloud are sorted. |
5 | 885 |
* |
886 |
* @since 2.8.0 |
|
887 |
* |
|
9 | 888 |
* @param WP_Term[] $tags Ordered array of terms. |
889 |
* @param array $args An array of tag cloud arguments. |
|
5 | 890 |
*/ |
0 | 891 |
$tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args ); |
5 | 892 |
if ( empty( $tags_sorted ) ) { |
893 |
return $return; |
|
894 |
} |
|
895 |
||
896 |
if ( $tags_sorted !== $tags ) { |
|
0 | 897 |
$tags = $tags_sorted; |
5 | 898 |
unset( $tags_sorted ); |
0 | 899 |
} else { |
5 | 900 |
if ( 'RAND' === $args['order'] ) { |
901 |
shuffle( $tags ); |
|
0 | 902 |
} else { |
903 |
// SQL cannot save you; this is a second (potentially different) sort on a subset of data. |
|
5 | 904 |
if ( 'name' === $args['orderby'] ) { |
0 | 905 |
uasort( $tags, '_wp_object_name_sort_cb' ); |
5 | 906 |
} else { |
0 | 907 |
uasort( $tags, '_wp_object_count_sort_cb' ); |
5 | 908 |
} |
0 | 909 |
|
5 | 910 |
if ( 'DESC' === $args['order'] ) { |
0 | 911 |
$tags = array_reverse( $tags, true ); |
5 | 912 |
} |
0 | 913 |
} |
914 |
} |
|
915 |
||
9 | 916 |
if ( $args['number'] > 0 ) { |
5 | 917 |
$tags = array_slice( $tags, 0, $args['number'] ); |
9 | 918 |
} |
0 | 919 |
|
9 | 920 |
$counts = array(); |
16 | 921 |
$real_counts = array(); // For the alt tag. |
0 | 922 |
foreach ( (array) $tags as $key => $tag ) { |
923 |
$real_counts[ $key ] = $tag->count; |
|
9 | 924 |
$counts[ $key ] = call_user_func( $args['topic_count_scale_callback'], $tag->count ); |
0 | 925 |
} |
926 |
||
927 |
$min_count = min( $counts ); |
|
9 | 928 |
$spread = max( $counts ) - $min_count; |
929 |
if ( $spread <= 0 ) { |
|
0 | 930 |
$spread = 1; |
9 | 931 |
} |
5 | 932 |
$font_spread = $args['largest'] - $args['smallest']; |
9 | 933 |
if ( $font_spread < 0 ) { |
0 | 934 |
$font_spread = 1; |
9 | 935 |
} |
0 | 936 |
$font_step = $font_spread / $spread; |
937 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
938 |
$aria_label = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
939 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
940 |
* Determine whether to output an 'aria-label' attribute with the tag name and count. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
941 |
* When tags have a different font size, they visually convey an important information |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
942 |
* that should be available to assistive technologies too. On the other hand, sometimes |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
943 |
* themes set up the Tag Cloud to display all tags with the same font size (setting |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
944 |
* the 'smallest' and 'largest' arguments to the same value). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
945 |
* In order to always serve the same content to all users, the 'aria-label' gets printed out: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
946 |
* - when tags have a different size |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
947 |
* - when the tag count is displayed (for example when users check the checkbox in the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
948 |
* Tag Cloud widget), regardless of the tags font size |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
949 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
950 |
if ( $args['show_count'] || 0 !== $font_spread ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
951 |
$aria_label = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
952 |
} |
0 | 953 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
954 |
// Assemble the data that will be used to generate the tag cloud markup. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
955 |
$tags_data = array(); |
0 | 956 |
foreach ( $tags as $key => $tag ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
957 |
$tag_id = isset( $tag->id ) ? $tag->id : $key; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
958 |
|
9 | 959 |
$count = $counts[ $key ]; |
0 | 960 |
$real_count = $real_counts[ $key ]; |
5 | 961 |
|
962 |
if ( $translate_nooped_plural ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
963 |
$formatted_count = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) ); |
5 | 964 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
965 |
$formatted_count = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args ); |
5 | 966 |
} |
967 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
968 |
$tags_data[] = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
969 |
'id' => $tag_id, |
16 | 970 |
'url' => ( '#' !== $tag->link ) ? $tag->link : '#', |
971 |
'role' => ( '#' !== $tag->link ) ? '' : ' role="button"', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
972 |
'name' => $tag->name, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
973 |
'formatted_count' => $formatted_count, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
974 |
'slug' => $tag->slug, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
975 |
'real_count' => $real_count, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
976 |
'class' => 'tag-cloud-link tag-link-' . $tag_id, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
977 |
'font_size' => $args['smallest'] + ( $count - $min_count ) * $font_step, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
978 |
'aria_label' => $aria_label ? sprintf( ' aria-label="%1$s (%2$s)"', esc_attr( $tag->name ), esc_attr( $formatted_count ) ) : '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
979 |
'show_count' => $args['show_count'] ? '<span class="tag-link-count"> (' . $real_count . ')</span>' : '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
980 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
981 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
982 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
983 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
984 |
* Filters the data used to generate the tag cloud. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
985 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
986 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
987 |
* |
18 | 988 |
* @param array[] $tags_data An array of term data arrays for terms used to generate the tag cloud. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
989 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
990 |
$tags_data = apply_filters( 'wp_generate_tag_cloud_data', $tags_data ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
991 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
992 |
$a = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
993 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
994 |
// Generate the output links array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
995 |
foreach ( $tags_data as $key => $tag_data ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
996 |
$class = $tag_data['class'] . ' tag-link-position-' . ( $key + 1 ); |
9 | 997 |
$a[] = sprintf( |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
998 |
'<a href="%1$s"%2$s class="%3$s" style="font-size: %4$s;"%5$s>%6$s%7$s</a>', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
999 |
esc_url( $tag_data['url'] ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1000 |
$tag_data['role'], |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1001 |
esc_attr( $class ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1002 |
esc_attr( str_replace( ',', '.', $tag_data['font_size'] ) . $args['unit'] ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1003 |
$tag_data['aria_label'], |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1004 |
esc_html( $tag_data['name'] ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1005 |
$tag_data['show_count'] |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1006 |
); |
0 | 1007 |
} |
1008 |
||
5 | 1009 |
switch ( $args['format'] ) { |
9 | 1010 |
case 'array': |
5 | 1011 |
$return =& $a; |
1012 |
break; |
|
9 | 1013 |
case 'list': |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1014 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1015 |
* Force role="list", as some browsers (sic: Safari 10) don't expose to assistive |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1016 |
* technologies the default role when the list is styled with `list-style: none`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1017 |
* Note: this is redundant but doesn't harm. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1018 |
*/ |
9 | 1019 |
$return = "<ul class='wp-tag-cloud' role='list'>\n\t<li>"; |
18 | 1020 |
$return .= implode( "</li>\n\t<li>", $a ); |
5 | 1021 |
$return .= "</li>\n</ul>\n"; |
1022 |
break; |
|
9 | 1023 |
default: |
18 | 1024 |
$return = implode( $args['separator'], $a ); |
5 | 1025 |
break; |
1026 |
} |
|
0 | 1027 |
|
5 | 1028 |
if ( $args['filter'] ) { |
1029 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1030 |
* Filters the generated output of a tag cloud. |
5 | 1031 |
* |
1032 |
* The filter is only evaluated if a true value is passed |
|
1033 |
* to the $filter argument in wp_generate_tag_cloud(). |
|
1034 |
* |
|
1035 |
* @since 2.3.0 |
|
1036 |
* |
|
1037 |
* @see wp_generate_tag_cloud() |
|
1038 |
* |
|
18 | 1039 |
* @param string[]|string $return String containing the generated HTML tag cloud output |
1040 |
* or an array of tag links if the 'format' argument |
|
1041 |
* equals 'array'. |
|
1042 |
* @param WP_Term[] $tags An array of terms used in the tag cloud. |
|
1043 |
* @param array $args An array of wp_generate_tag_cloud() arguments. |
|
5 | 1044 |
*/ |
0 | 1045 |
return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args ); |
9 | 1046 |
} else { |
1047 |
return $return; |
|
5 | 1048 |
} |
0 | 1049 |
} |
1050 |
||
1051 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1052 |
* Serves as a callback for comparing objects based on name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1053 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1054 |
* Used with `uasort()`. |
0 | 1055 |
* |
1056 |
* @since 3.1.0 |
|
1057 |
* @access private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1058 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1059 |
* @param object $a The first object to compare. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1060 |
* @param object $b The second object to compare. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1061 |
* @return int Negative number if `$a->name` is less than `$b->name`, zero if they are equal, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1062 |
* or greater than zero if `$a->name` is greater than `$b->name`. |
0 | 1063 |
*/ |
1064 |
function _wp_object_name_sort_cb( $a, $b ) { |
|
1065 |
return strnatcasecmp( $a->name, $b->name ); |
|
1066 |
} |
|
1067 |
||
1068 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1069 |
* Serves as a callback for comparing objects based on count. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1070 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1071 |
* Used with `uasort()`. |
0 | 1072 |
* |
1073 |
* @since 3.1.0 |
|
1074 |
* @access private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1075 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1076 |
* @param object $a The first object to compare. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1077 |
* @param object $b The second object to compare. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1078 |
* @return bool Whether the count value for `$a` is greater than the count value for `$b`. |
0 | 1079 |
*/ |
1080 |
function _wp_object_count_sort_cb( $a, $b ) { |
|
1081 |
return ( $a->count > $b->count ); |
|
1082 |
} |
|
1083 |
||
1084 |
// |
|
16 | 1085 |
// Helper functions. |
0 | 1086 |
// |
1087 |
||
1088 |
/** |
|
16 | 1089 |
* Retrieves HTML list content for category list. |
1090 |
* |
|
1091 |
* @since 2.1.0 |
|
1092 |
* @since 5.3.0 Formalized the existing `...$args` parameter by adding it |
|
1093 |
* to the function signature. |
|
0 | 1094 |
* |
1095 |
* @uses Walker_Category to create HTML list content. |
|
16 | 1096 |
* @see Walker::walk() for parameters and return description. |
1097 |
* |
|
1098 |
* @param mixed ...$args Elements array, maximum hierarchical depth and optional additional arguments. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1099 |
* @return string |
0 | 1100 |
*/ |
16 | 1101 |
function walk_category_tree( ...$args ) { |
1102 |
// The user's options are the third parameter. |
|
5 | 1103 |
if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) { |
0 | 1104 |
$walker = new Walker_Category; |
5 | 1105 |
} else { |
16 | 1106 |
/** |
1107 |
* @var Walker $walker |
|
1108 |
*/ |
|
0 | 1109 |
$walker = $args[2]['walker']; |
5 | 1110 |
} |
16 | 1111 |
return $walker->walk( ...$args ); |
0 | 1112 |
} |
1113 |
||
1114 |
/** |
|
16 | 1115 |
* Retrieves HTML dropdown (select) content for category list. |
1116 |
* |
|
1117 |
* @since 2.1.0 |
|
1118 |
* @since 5.3.0 Formalized the existing `...$args` parameter by adding it |
|
1119 |
* to the function signature. |
|
0 | 1120 |
* |
1121 |
* @uses Walker_CategoryDropdown to create HTML dropdown content. |
|
16 | 1122 |
* @see Walker::walk() for parameters and return description. |
1123 |
* |
|
1124 |
* @param mixed ...$args Elements array, maximum hierarchical depth and optional additional arguments. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1125 |
* @return string |
0 | 1126 |
*/ |
16 | 1127 |
function walk_category_dropdown_tree( ...$args ) { |
1128 |
// The user's options are the third parameter. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1129 |
if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) { |
0 | 1130 |
$walker = new Walker_CategoryDropdown; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1131 |
} else { |
16 | 1132 |
/** |
1133 |
* @var Walker $walker |
|
1134 |
*/ |
|
0 | 1135 |
$walker = $args[2]['walker']; |
1136 |
} |
|
16 | 1137 |
return $walker->walk( ...$args ); |
0 | 1138 |
} |
1139 |
||
1140 |
// |
|
16 | 1141 |
// Tags. |
0 | 1142 |
// |
1143 |
||
1144 |
/** |
|
16 | 1145 |
* Retrieves the link to the tag. |
0 | 1146 |
* |
1147 |
* @since 2.3.0 |
|
16 | 1148 |
* |
0 | 1149 |
* @see get_term_link() |
1150 |
* |
|
1151 |
* @param int|object $tag Tag ID or object. |
|
1152 |
* @return string Link on success, empty string if tag does not exist. |
|
1153 |
*/ |
|
1154 |
function get_tag_link( $tag ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1155 |
return get_category_link( $tag ); |
0 | 1156 |
} |
1157 |
||
1158 |
/** |
|
16 | 1159 |
* Retrieves the tags for a post. |
0 | 1160 |
* |
1161 |
* @since 2.3.0 |
|
1162 |
* |
|
18 | 1163 |
* @param int|WP_Post $post_id Post ID or object. |
1164 |
* @return WP_Term[]|false|WP_Error Array of WP_Term objects on success, false if there are no terms |
|
1165 |
* or the post does not exist, WP_Error on failure. |
|
0 | 1166 |
*/ |
16 | 1167 |
function get_the_tags( $post_id = 0 ) { |
1168 |
$terms = get_the_terms( $post_id, 'post_tag' ); |
|
5 | 1169 |
|
1170 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1171 |
* Filters the array of tags for the given post. |
5 | 1172 |
* |
1173 |
* @since 2.3.0 |
|
1174 |
* |
|
1175 |
* @see get_the_terms() |
|
1176 |
* |
|
18 | 1177 |
* @param WP_Term[]|false|WP_Error $terms Array of WP_Term objects on success, false if there are no terms |
1178 |
* or the post does not exist, WP_Error on failure. |
|
5 | 1179 |
*/ |
16 | 1180 |
return apply_filters( 'get_the_tags', $terms ); |
0 | 1181 |
} |
1182 |
||
1183 |
/** |
|
16 | 1184 |
* Retrieves the tags for a post formatted as a string. |
0 | 1185 |
* |
1186 |
* @since 2.3.0 |
|
1187 |
* |
|
16 | 1188 |
* @param string $before Optional. String to use before the tags. Default empty. |
1189 |
* @param string $sep Optional. String to use between the tags. Default empty. |
|
1190 |
* @param string $after Optional. String to use after the tags. Default empty. |
|
1191 |
* @param int $post_id Optional. Post ID. Defaults to the current post ID. |
|
1192 |
* @return string|false|WP_Error A list of tags on success, false if there are no terms, |
|
1193 |
* WP_Error on failure. |
|
0 | 1194 |
*/ |
16 | 1195 |
function get_the_tag_list( $before = '', $sep = '', $after = '', $post_id = 0 ) { |
1196 |
$tag_list = get_the_term_list( $post_id, 'post_tag', $before, $sep, $after ); |
|
5 | 1197 |
|
1198 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1199 |
* Filters the tags list for a given post. |
5 | 1200 |
* |
1201 |
* @since 2.3.0 |
|
1202 |
* |
|
1203 |
* @param string $tag_list List of tags. |
|
16 | 1204 |
* @param string $before String to use before the tags. |
5 | 1205 |
* @param string $sep String to use between the tags. |
16 | 1206 |
* @param string $after String to use after the tags. |
1207 |
* @param int $post_id Post ID. |
|
5 | 1208 |
*/ |
16 | 1209 |
return apply_filters( 'the_tags', $tag_list, $before, $sep, $after, $post_id ); |
0 | 1210 |
} |
1211 |
||
1212 |
/** |
|
16 | 1213 |
* Displays the tags for a post. |
0 | 1214 |
* |
1215 |
* @since 2.3.0 |
|
1216 |
* |
|
16 | 1217 |
* @param string $before Optional. String to use before the tags. Defaults to 'Tags:'. |
1218 |
* @param string $sep Optional. String to use between the tags. Default ', '. |
|
1219 |
* @param string $after Optional. String to use after the tags. Default empty. |
|
0 | 1220 |
*/ |
1221 |
function the_tags( $before = null, $sep = ', ', $after = '' ) { |
|
9 | 1222 |
if ( null === $before ) { |
1223 |
$before = __( 'Tags: ' ); |
|
1224 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1225 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1226 |
$the_tags = get_the_tag_list( $before, $sep, $after ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1227 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1228 |
if ( ! is_wp_error( $the_tags ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1229 |
echo $the_tags; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1230 |
} |
0 | 1231 |
} |
1232 |
||
1233 |
/** |
|
16 | 1234 |
* Retrieves tag description. |
0 | 1235 |
* |
5 | 1236 |
* @since 2.8.0 |
0 | 1237 |
* |
16 | 1238 |
* @param int $tag Optional. Tag ID. Defaults to the current tag ID. |
1239 |
* @return string Tag description, if available. |
|
0 | 1240 |
*/ |
1241 |
function tag_description( $tag = 0 ) { |
|
1242 |
return term_description( $tag ); |
|
1243 |
} |
|
1244 |
||
1245 |
/** |
|
16 | 1246 |
* Retrieves term description. |
0 | 1247 |
* |
5 | 1248 |
* @since 2.8.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1249 |
* @since 4.9.2 The `$taxonomy` parameter was deprecated. |
0 | 1250 |
* |
16 | 1251 |
* @param int $term Optional. Term ID. Defaults to the current term ID. |
1252 |
* @param null $deprecated Deprecated. Not used. |
|
1253 |
* @return string Term description, if available. |
|
0 | 1254 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1255 |
function term_description( $term = 0, $deprecated = null ) { |
0 | 1256 |
if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) { |
1257 |
$term = get_queried_object(); |
|
1258 |
if ( $term ) { |
|
1259 |
$term = $term->term_id; |
|
1260 |
} |
|
1261 |
} |
|
16 | 1262 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1263 |
$description = get_term_field( 'description', $term ); |
16 | 1264 |
|
0 | 1265 |
return is_wp_error( $description ) ? '' : $description; |
1266 |
} |
|
1267 |
||
1268 |
/** |
|
16 | 1269 |
* Retrieves the terms of the taxonomy that are attached to the post. |
0 | 1270 |
* |
1271 |
* @since 2.5.0 |
|
1272 |
* |
|
9 | 1273 |
* @param int|WP_Post $post Post ID or object. |
1274 |
* @param string $taxonomy Taxonomy name. |
|
1275 |
* @return WP_Term[]|false|WP_Error Array of WP_Term objects on success, false if there are no terms |
|
1276 |
* or the post does not exist, WP_Error on failure. |
|
0 | 1277 |
*/ |
1278 |
function get_the_terms( $post, $taxonomy ) { |
|
16 | 1279 |
$post = get_post( $post ); |
1280 |
if ( ! $post ) { |
|
0 | 1281 |
return false; |
9 | 1282 |
} |
0 | 1283 |
|
1284 |
$terms = get_object_term_cache( $post->ID, $taxonomy ); |
|
1285 |
if ( false === $terms ) { |
|
1286 |
$terms = wp_get_object_terms( $post->ID, $taxonomy ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1287 |
if ( ! is_wp_error( $terms ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1288 |
$term_ids = wp_list_pluck( $terms, 'term_id' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1289 |
wp_cache_add( $post->ID, $term_ids, $taxonomy . '_relationships' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1290 |
} |
0 | 1291 |
} |
1292 |
||
5 | 1293 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1294 |
* Filters the list of terms attached to the given post. |
5 | 1295 |
* |
1296 |
* @since 3.1.0 |
|
1297 |
* |
|
9 | 1298 |
* @param WP_Term[]|WP_Error $terms Array of attached terms, or WP_Error on failure. |
1299 |
* @param int $post_id Post ID. |
|
1300 |
* @param string $taxonomy Name of the taxonomy. |
|
5 | 1301 |
*/ |
0 | 1302 |
$terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy ); |
1303 |
||
9 | 1304 |
if ( empty( $terms ) ) { |
0 | 1305 |
return false; |
9 | 1306 |
} |
0 | 1307 |
|
1308 |
return $terms; |
|
1309 |
} |
|
1310 |
||
1311 |
/** |
|
16 | 1312 |
* Retrieves a post's terms as a list with specified format. |
1313 |
* |
|
1314 |
* Terms are linked to their respective term listing pages. |
|
0 | 1315 |
* |
1316 |
* @since 2.5.0 |
|
1317 |
* |
|
16 | 1318 |
* @param int $post_id Post ID. |
0 | 1319 |
* @param string $taxonomy Taxonomy name. |
16 | 1320 |
* @param string $before Optional. String to use before the terms. Default empty. |
1321 |
* @param string $sep Optional. String to use between the terms. Default empty. |
|
1322 |
* @param string $after Optional. String to use after the terms. Default empty. |
|
1323 |
* @return string|false|WP_Error A list of terms on success, false if there are no terms, |
|
1324 |
* WP_Error on failure. |
|
0 | 1325 |
*/ |
16 | 1326 |
function get_the_term_list( $post_id, $taxonomy, $before = '', $sep = '', $after = '' ) { |
1327 |
$terms = get_the_terms( $post_id, $taxonomy ); |
|
0 | 1328 |
|
9 | 1329 |
if ( is_wp_error( $terms ) ) { |
0 | 1330 |
return $terms; |
9 | 1331 |
} |
0 | 1332 |
|
9 | 1333 |
if ( empty( $terms ) ) { |
0 | 1334 |
return false; |
9 | 1335 |
} |
0 | 1336 |
|
5 | 1337 |
$links = array(); |
1338 |
||
0 | 1339 |
foreach ( $terms as $term ) { |
1340 |
$link = get_term_link( $term, $taxonomy ); |
|
5 | 1341 |
if ( is_wp_error( $link ) ) { |
0 | 1342 |
return $link; |
5 | 1343 |
} |
1344 |
$links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>'; |
|
0 | 1345 |
} |
1346 |
||
5 | 1347 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1348 |
* Filters the term links for a given taxonomy. |
5 | 1349 |
* |
19 | 1350 |
* The dynamic portion of the hook name, `$taxonomy`, refers |
5 | 1351 |
* to the taxonomy slug. |
1352 |
* |
|
19 | 1353 |
* Possible hook names include: |
1354 |
* |
|
1355 |
* - `term_links-category` |
|
1356 |
* - `term_links-post_tag` |
|
1357 |
* - `term_links-post_format` |
|
1358 |
* |
|
5 | 1359 |
* @since 2.5.0 |
1360 |
* |
|
9 | 1361 |
* @param string[] $links An array of term links. |
5 | 1362 |
*/ |
16 | 1363 |
$term_links = apply_filters( "term_links-{$taxonomy}", $links ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
0 | 1364 |
|
18 | 1365 |
return $before . implode( $sep, $term_links ) . $after; |
0 | 1366 |
} |
1367 |
||
1368 |
/** |
|
16 | 1369 |
* Retrieves term parents with separator. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1370 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1371 |
* @since 4.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1372 |
* |
16 | 1373 |
* @param int $term_id Term ID. |
1374 |
* @param string $taxonomy Taxonomy name. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1375 |
* @param string|array $args { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1376 |
* Array of optional arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1377 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1378 |
* @type string $format Use term names or slugs for display. Accepts 'name' or 'slug'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1379 |
* Default 'name'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1380 |
* @type string $separator Separator for between the terms. Default '/'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1381 |
* @type bool $link Whether to format as a link. Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1382 |
* @type bool $inclusive Include the term to get the parents for. Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1383 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1384 |
* @return string|WP_Error A list of term parents on success, WP_Error or empty string on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1385 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1386 |
function get_term_parents_list( $term_id, $taxonomy, $args = array() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1387 |
$list = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1388 |
$term = get_term( $term_id, $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1389 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1390 |
if ( is_wp_error( $term ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1391 |
return $term; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1392 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1393 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1394 |
if ( ! $term ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1395 |
return $list; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1396 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1397 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1398 |
$term_id = $term->term_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1399 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1400 |
$defaults = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1401 |
'format' => 'name', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1402 |
'separator' => '/', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1403 |
'link' => true, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1404 |
'inclusive' => true, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1405 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1406 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1407 |
$args = wp_parse_args( $args, $defaults ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1408 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1409 |
foreach ( array( 'link', 'inclusive' ) as $bool ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1410 |
$args[ $bool ] = wp_validate_boolean( $args[ $bool ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1411 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1412 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1413 |
$parents = get_ancestors( $term_id, $taxonomy, 'taxonomy' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1414 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1415 |
if ( $args['inclusive'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1416 |
array_unshift( $parents, $term_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1417 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1418 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1419 |
foreach ( array_reverse( $parents ) as $term_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1420 |
$parent = get_term( $term_id, $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1421 |
$name = ( 'slug' === $args['format'] ) ? $parent->slug : $parent->name; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1422 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1423 |
if ( $args['link'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1424 |
$list .= '<a href="' . esc_url( get_term_link( $parent->term_id, $taxonomy ) ) . '">' . $name . '</a>' . $args['separator']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1425 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1426 |
$list .= $name . $args['separator']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1427 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1428 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1429 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1430 |
return $list; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1431 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1432 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1433 |
/** |
16 | 1434 |
* Displays the terms for a post in a list. |
0 | 1435 |
* |
1436 |
* @since 2.5.0 |
|
1437 |
* |
|
16 | 1438 |
* @param int $post_id Post ID. |
0 | 1439 |
* @param string $taxonomy Taxonomy name. |
16 | 1440 |
* @param string $before Optional. String to use before the terms. Default empty. |
1441 |
* @param string $sep Optional. String to use between the terms. Default ', '. |
|
1442 |
* @param string $after Optional. String to use after the terms. Default empty. |
|
1443 |
* @return void|false Void on success, false on failure. |
|
0 | 1444 |
*/ |
16 | 1445 |
function the_terms( $post_id, $taxonomy, $before = '', $sep = ', ', $after = '' ) { |
1446 |
$term_list = get_the_term_list( $post_id, $taxonomy, $before, $sep, $after ); |
|
0 | 1447 |
|
9 | 1448 |
if ( is_wp_error( $term_list ) ) { |
0 | 1449 |
return false; |
9 | 1450 |
} |
0 | 1451 |
|
5 | 1452 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1453 |
* Filters the list of terms to display. |
5 | 1454 |
* |
1455 |
* @since 2.9.0 |
|
1456 |
* |
|
9 | 1457 |
* @param string $term_list List of terms to display. |
5 | 1458 |
* @param string $taxonomy The taxonomy name. |
1459 |
* @param string $before String to use before the terms. |
|
1460 |
* @param string $sep String to use between the terms. |
|
1461 |
* @param string $after String to use after the terms. |
|
1462 |
*/ |
|
1463 |
echo apply_filters( 'the_terms', $term_list, $taxonomy, $before, $sep, $after ); |
|
0 | 1464 |
} |
1465 |
||
1466 |
/** |
|
16 | 1467 |
* Checks if the current post has any of given category. |
1468 |
* |
|
1469 |
* The given categories are checked against the post's categories' term_ids, names and slugs. |
|
1470 |
* Categories given as integers will only be checked against the post's categories' term_ids. |
|
1471 |
* |
|
1472 |
* If no categories are given, determines if post has any categories. |
|
0 | 1473 |
* |
1474 |
* @since 3.1.0 |
|
1475 |
* |
|
16 | 1476 |
* @param string|int|array $category Optional. The category name/term_id/slug, |
1477 |
* or an array of them to check for. Default empty. |
|
1478 |
* @param int|object $post Optional. Post to check instead of the current post. |
|
1479 |
* @return bool True if the current post has any of the given categories |
|
1480 |
* (or any category, if no category specified). False otherwise. |
|
0 | 1481 |
*/ |
1482 |
function has_category( $category = '', $post = null ) { |
|
1483 |
return has_term( $category, 'category', $post ); |
|
1484 |
} |
|
1485 |
||
1486 |
/** |
|
9 | 1487 |
* Checks if the current post has any of given tags. |
0 | 1488 |
* |
1489 |
* The given tags are checked against the post's tags' term_ids, names and slugs. |
|
1490 |
* Tags given as integers will only be checked against the post's tags' term_ids. |
|
16 | 1491 |
* |
0 | 1492 |
* If no tags are given, determines if post has any tags. |
1493 |
* |
|
9 | 1494 |
* For more information on this and similar theme functions, check out |
1495 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
1496 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
1497 |
* |
|
0 | 1498 |
* @since 2.6.0 |
16 | 1499 |
* @since 2.7.0 Tags given as integers are only checked against |
1500 |
* the post's tags' term_ids, not names or slugs. |
|
1501 |
* @since 2.7.0 Can be used outside of the WordPress Loop if `$post` is provided. |
|
0 | 1502 |
* |
16 | 1503 |
* @param string|int|array $tag Optional. The tag name/term_id/slug, |
1504 |
* or an array of them to check for. Default empty. |
|
1505 |
* @param int|object $post Optional. Post to check instead of the current post. |
|
1506 |
* @return bool True if the current post has any of the given tags |
|
1507 |
* (or any tag, if no tag specified). False otherwise. |
|
0 | 1508 |
*/ |
1509 |
function has_tag( $tag = '', $post = null ) { |
|
1510 |
return has_term( $tag, 'post_tag', $post ); |
|
1511 |
} |
|
1512 |
||
1513 |
/** |
|
16 | 1514 |
* Checks if the current post has any of given terms. |
0 | 1515 |
* |
1516 |
* The given terms are checked against the post's terms' term_ids, names and slugs. |
|
1517 |
* Terms given as integers will only be checked against the post's terms' term_ids. |
|
16 | 1518 |
* |
0 | 1519 |
* If no terms are given, determines if post has any terms. |
1520 |
* |
|
1521 |
* @since 3.1.0 |
|
1522 |
* |
|
16 | 1523 |
* @param string|int|array $term Optional. The term name/term_id/slug, |
1524 |
* or an array of them to check for. Default empty. |
|
1525 |
* @param string $taxonomy Optional. Taxonomy name. Default empty. |
|
1526 |
* @param int|WP_Post $post Optional. Post to check instead of the current post. |
|
1527 |
* @return bool True if the current post has any of the given terms |
|
1528 |
* (or any term, if no term specified). False otherwise. |
|
0 | 1529 |
*/ |
1530 |
function has_term( $term = '', $taxonomy = '', $post = null ) { |
|
9 | 1531 |
$post = get_post( $post ); |
0 | 1532 |
|
9 | 1533 |
if ( ! $post ) { |
0 | 1534 |
return false; |
9 | 1535 |
} |
0 | 1536 |
|
1537 |
$r = is_object_in_term( $post->ID, $taxonomy, $term ); |
|
9 | 1538 |
if ( is_wp_error( $r ) ) { |
0 | 1539 |
return false; |
9 | 1540 |
} |
0 | 1541 |
|
1542 |
return $r; |
|
1543 |
} |