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