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