mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * Category Template Tags and API.
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Template
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 /**
mp-wp_genesis 10 * Retrieve category children list separated before and after the term IDs.
mp-wp_genesis 11 *
mp-wp_genesis 12 * @since 1.2.0
mp-wp_genesis 13 *
mp-wp_genesis 14 * @param int $id Category ID to retrieve children.
mp-wp_genesis 15 * @param string $before Optional. Prepend before category term ID.
mp-wp_genesis 16 * @param string $after Optional, default is empty string. Append after category term ID.
mp-wp_genesis 17 * @param array $visited Optional. Category Term IDs that have already been added.
mp-wp_genesis 18 * @return string
mp-wp_genesis 19 */
mp-wp_genesis 20 function get_category_children( $id, $before = '/', $after = '', $visited = array() ) {
mp-wp_genesis 21 if ( 0 == $id )
mp-wp_genesis 22 return '';
mp-wp_genesis 23
mp-wp_genesis 24 $chain = '';
mp-wp_genesis 25 /** TODO: consult hierarchy */
mp-wp_genesis 26 $cat_ids = get_all_category_ids();
mp-wp_genesis 27 foreach ( (array) $cat_ids as $cat_id ) {
mp-wp_genesis 28 if ( $cat_id == $id )
mp-wp_genesis 29 continue;
mp-wp_genesis 30
mp-wp_genesis 31 $category = get_category( $cat_id );
mp-wp_genesis 32 if ( is_wp_error( $category ) )
mp-wp_genesis 33 return $category;
mp-wp_genesis 34 if ( $category->parent == $id && !in_array( $category->term_id, $visited ) ) {
mp-wp_genesis 35 $visited[] = $category->term_id;
mp-wp_genesis 36 $chain .= $before.$category->term_id.$after;
mp-wp_genesis 37 $chain .= get_category_children( $category->term_id, $before, $after );
mp-wp_genesis 38 }
mp-wp_genesis 39 }
mp-wp_genesis 40 return $chain;
mp-wp_genesis 41 }
mp-wp_genesis 42
mp-wp_genesis 43 /**
mp-wp_genesis 44 * Retrieve category link URL.
mp-wp_genesis 45 *
mp-wp_genesis 46 * @since 1.0.0
mp-wp_genesis 47 * @uses apply_filters() Calls 'category_link' filter on category link and category ID.
mp-wp_genesis 48 *
mp-wp_genesis 49 * @param int $category_id Category ID.
mp-wp_genesis 50 * @return string
mp-wp_genesis 51 */
mp-wp_genesis 52 function get_category_link( $category_id ) {
mp-wp_genesis 53 global $wp_rewrite;
mp-wp_genesis 54 $catlink = $wp_rewrite->get_category_permastruct();
mp-wp_genesis 55
mp-wp_genesis 56 if ( empty( $catlink ) ) {
mp-wp_genesis 57 $file = get_option( 'home' ) . '/';
mp-wp_genesis 58 $catlink = $file . '?cat=' . $category_id;
mp-wp_genesis 59 } else {
mp-wp_genesis 60 $category = &get_category( $category_id );
mp-wp_genesis 61 if ( is_wp_error( $category ) )
mp-wp_genesis 62 return $category;
mp-wp_genesis 63 $category_nicename = $category->slug;
mp-wp_genesis 64
mp-wp_genesis 65 if ( $category->parent == $category_id ) // recursive recursion
mp-wp_genesis 66 $category->parent = 0;
mp-wp_genesis 67 elseif ($category->parent != 0 )
mp-wp_genesis 68 $category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
mp-wp_genesis 69
mp-wp_genesis 70 $catlink = str_replace( '%category%', $category_nicename, $catlink );
mp-wp_genesis 71 $catlink = get_option( 'home' ) . user_trailingslashit( $catlink, 'category' );
mp-wp_genesis 72 }
mp-wp_genesis 73 return apply_filters( 'category_link', $catlink, $category_id );
mp-wp_genesis 74 }
mp-wp_genesis 75
mp-wp_genesis 76 /**
mp-wp_genesis 77 * Retrieve category parents with separator.
mp-wp_genesis 78 *
mp-wp_genesis 79 * @since 1.2.0
mp-wp_genesis 80 *
mp-wp_genesis 81 * @param int $id Category ID.
mp-wp_genesis 82 * @param bool $link Optional, default is false. Whether to format with link.
mp-wp_genesis 83 * @param string $separator Optional, default is '/'. How to separate categories.
mp-wp_genesis 84 * @param bool $nicename Optional, default is false. Whether to use nice name for display.
mp-wp_genesis 85 * @param array $visited Optional. Already linked to categories to prevent duplicates.
mp-wp_genesis 86 * @return string
mp-wp_genesis 87 */
mp-wp_genesis 88 function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
mp-wp_genesis 89 $chain = '';
mp-wp_genesis 90 $parent = &get_category( $id );
mp-wp_genesis 91 if ( is_wp_error( $parent ) )
mp-wp_genesis 92 return $parent;
mp-wp_genesis 93
mp-wp_genesis 94 if ( $nicename )
mp-wp_genesis 95 $name = $parent->slug;
mp-wp_genesis 96 else
mp-wp_genesis 97 $name = $parent->cat_name;
mp-wp_genesis 98
mp-wp_genesis 99 if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
mp-wp_genesis 100 $visited[] = $parent->parent;
mp-wp_genesis 101 $chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
mp-wp_genesis 102 }
mp-wp_genesis 103
mp-wp_genesis 104 if ( $link )
mp-wp_genesis 105 $chain .= '<a href="' . get_category_link( $parent->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $parent->cat_name ) . '">'.$name.'</a>' . $separator;
mp-wp_genesis 106 else
mp-wp_genesis 107 $chain .= $name.$separator;
mp-wp_genesis 108 return $chain;
mp-wp_genesis 109 }
mp-wp_genesis 110
mp-wp_genesis 111 /**
mp-wp_genesis 112 * Retrieve post categories.
mp-wp_genesis 113 *
mp-wp_genesis 114 * @since 0.71
mp-wp_genesis 115 * @uses $post
mp-wp_genesis 116 *
mp-wp_genesis 117 * @param int $id Optional, default to current post ID. The post ID.
mp-wp_genesis 118 * @return array
mp-wp_genesis 119 */
mp-wp_genesis 120 function get_the_category( $id = false ) {
mp-wp_genesis 121 global $post;
mp-wp_genesis 122
mp-wp_genesis 123 $id = (int) $id;
mp-wp_genesis 124 if ( !$id )
mp-wp_genesis 125 $id = (int) $post->ID;
mp-wp_genesis 126
mp-wp_genesis 127 $categories = get_object_term_cache( $id, 'category' );
mp-wp_genesis 128 if ( false === $categories ) {
mp-wp_genesis 129 $categories = wp_get_object_terms( $id, 'category' );
mp-wp_genesis 130 wp_cache_add($id, $categories, 'category_relationships');
mp-wp_genesis 131 }
mp-wp_genesis 132
mp-wp_genesis 133 if ( !empty( $categories ) )
mp-wp_genesis 134 usort( $categories, '_usort_terms_by_name' );
mp-wp_genesis 135 else
mp-wp_genesis 136 $categories = array();
mp-wp_genesis 137
mp-wp_genesis 138 foreach ( (array) array_keys( $categories ) as $key ) {
mp-wp_genesis 139 _make_cat_compat( $categories[$key] );
mp-wp_genesis 140 }
mp-wp_genesis 141
mp-wp_genesis 142 return $categories;
mp-wp_genesis 143 }
mp-wp_genesis 144
mp-wp_genesis 145 /**
mp-wp_genesis 146 * Sort categories by name.
mp-wp_genesis 147 *
mp-wp_genesis 148 * Used by usort() as a callback, should not be used directly. Can actually be
mp-wp_genesis 149 * used to sort any term object.
mp-wp_genesis 150 *
mp-wp_genesis 151 * @since 2.3.0
mp-wp_genesis 152 * @access private
mp-wp_genesis 153 *
mp-wp_genesis 154 * @param object $a
mp-wp_genesis 155 * @param object $b
mp-wp_genesis 156 * @return int
mp-wp_genesis 157 */
mp-wp_genesis 158 function _usort_terms_by_name( $a, $b ) {
mp-wp_genesis 159 return strcmp( $a->name, $b->name );
mp-wp_genesis 160 }
mp-wp_genesis 161
mp-wp_genesis 162 /**
mp-wp_genesis 163 * Sort categories by ID.
mp-wp_genesis 164 *
mp-wp_genesis 165 * Used by usort() as a callback, should not be used directly. Can actually be
mp-wp_genesis 166 * used to sort any term object.
mp-wp_genesis 167 *
mp-wp_genesis 168 * @since 2.3.0
mp-wp_genesis 169 * @access private
mp-wp_genesis 170 *
mp-wp_genesis 171 * @param object $a
mp-wp_genesis 172 * @param object $b
mp-wp_genesis 173 * @return int
mp-wp_genesis 174 */
mp-wp_genesis 175 function _usort_terms_by_ID( $a, $b ) {
mp-wp_genesis 176 if ( $a->term_id > $b->term_id )
mp-wp_genesis 177 return 1;
mp-wp_genesis 178 elseif ( $a->term_id < $b->term_id )
mp-wp_genesis 179 return -1;
mp-wp_genesis 180 else
mp-wp_genesis 181 return 0;
mp-wp_genesis 182 }
mp-wp_genesis 183
mp-wp_genesis 184 /**
mp-wp_genesis 185 * Retrieve category name based on category ID.
mp-wp_genesis 186 *
mp-wp_genesis 187 * @since 0.71
mp-wp_genesis 188 *
mp-wp_genesis 189 * @param int $cat_ID Category ID.
mp-wp_genesis 190 * @return string Category name.
mp-wp_genesis 191 */
mp-wp_genesis 192 function get_the_category_by_ID( $cat_ID ) {
mp-wp_genesis 193 $cat_ID = (int) $cat_ID;
mp-wp_genesis 194 $category = &get_category( $cat_ID );
mp-wp_genesis 195 if ( is_wp_error( $category ) )
mp-wp_genesis 196 return $category;
mp-wp_genesis 197 return $category->name;
mp-wp_genesis 198 }
mp-wp_genesis 199
mp-wp_genesis 200 /**
mp-wp_genesis 201 * Retrieve category list in either HTML list or custom format.
mp-wp_genesis 202 *
mp-wp_genesis 203 * @since 1.5.1
mp-wp_genesis 204 *
mp-wp_genesis 205 * @param string $separator Optional, default is empty string. Separator for between the categories.
mp-wp_genesis 206 * @param string $parents Optional. How to display the parents.
mp-wp_genesis 207 * @param int $post_id Optional. Post ID to retrieve categories.
mp-wp_genesis 208 * @return string
mp-wp_genesis 209 */
mp-wp_genesis 210 function get_the_category_list( $separator = '', $parents='', $post_id = false ) {
mp-wp_genesis 211 global $wp_rewrite;
mp-wp_genesis 212 $categories = get_the_category( $post_id );
mp-wp_genesis 213 if ( empty( $categories ) )
mp-wp_genesis 214 return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
mp-wp_genesis 215
mp-wp_genesis 216 $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
mp-wp_genesis 217
mp-wp_genesis 218 $thelist = '';
mp-wp_genesis 219 if ( '' == $separator ) {
mp-wp_genesis 220 $thelist .= '<ul class="post-categories">';
mp-wp_genesis 221 foreach ( $categories as $category ) {
mp-wp_genesis 222 $thelist .= "\n\t<li>";
mp-wp_genesis 223 switch ( strtolower( $parents ) ) {
mp-wp_genesis 224 case 'multiple':
mp-wp_genesis 225 if ( $category->parent )
mp-wp_genesis 226 $thelist .= get_category_parents( $category->parent, true, $separator );
mp-wp_genesis 227 $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>' . $category->name.'</a></li>';
mp-wp_genesis 228 break;
mp-wp_genesis 229 case 'single':
mp-wp_genesis 230 $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>';
mp-wp_genesis 231 if ( $category->parent )
mp-wp_genesis 232 $thelist .= get_category_parents( $category->parent, false, $separator );
mp-wp_genesis 233 $thelist .= $category->name.'</a></li>';
mp-wp_genesis 234 break;
mp-wp_genesis 235 case '':
mp-wp_genesis 236 default:
mp-wp_genesis 237 $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>' . $category->cat_name.'</a></li>';
mp-wp_genesis 238 }
mp-wp_genesis 239 }
mp-wp_genesis 240 $thelist .= '</ul>';
mp-wp_genesis 241 } else {
mp-wp_genesis 242 $i = 0;
mp-wp_genesis 243 foreach ( $categories as $category ) {
mp-wp_genesis 244 if ( 0 < $i )
mp-wp_genesis 245 $thelist .= $separator . ' ';
mp-wp_genesis 246 switch ( strtolower( $parents ) ) {
mp-wp_genesis 247 case 'multiple':
mp-wp_genesis 248 if ( $category->parent )
mp-wp_genesis 249 $thelist .= get_category_parents( $category->parent, true, $separator );
mp-wp_genesis 250 $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>' . $category->cat_name.'</a>';
mp-wp_genesis 251 break;
mp-wp_genesis 252 case 'single':
mp-wp_genesis 253 $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>';
mp-wp_genesis 254 if ( $category->parent )
mp-wp_genesis 255 $thelist .= get_category_parents( $category->parent, false, $separator );
mp-wp_genesis 256 $thelist .= "$category->cat_name</a>";
mp-wp_genesis 257 break;
mp-wp_genesis 258 case '':
mp-wp_genesis 259 default:
mp-wp_genesis 260 $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>' . $category->name.'</a>';
mp-wp_genesis 261 }
mp-wp_genesis 262 ++$i;
mp-wp_genesis 263 }
mp-wp_genesis 264 }
mp-wp_genesis 265 return apply_filters( 'the_category', $thelist, $separator, $parents );
mp-wp_genesis 266 }
mp-wp_genesis 267
mp-wp_genesis 268
mp-wp_genesis 269 /**
mp-wp_genesis 270 * Check if the current post in within any of the given categories.
mp-wp_genesis 271 *
mp-wp_genesis 272 * The given categories are checked against the post's categories' term_ids, names and slugs.
mp-wp_genesis 273 * Categories given as integers will only be checked against the post's categories' term_ids.
mp-wp_genesis 274 *
mp-wp_genesis 275 * Prior to v2.5 of WordPress, category names were not supported.
mp-wp_genesis 276 * Prior to v2.7, category slugs were not supported.
mp-wp_genesis 277 * Prior to v2.7, only one category could be compared: in_category( $single_category ).
mp-wp_genesis 278 * Prior to v2.7, this function could only be used in the WordPress Loop.
mp-wp_genesis 279 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
mp-wp_genesis 280 *
mp-wp_genesis 281 * @since 1.2.0
mp-wp_genesis 282 *
mp-wp_genesis 283 * @uses is_object_in_term()
mp-wp_genesis 284 *
mp-wp_genesis 285 * @param int|string|array $category. Category ID, name or slug, or array of said.
mp-wp_genesis 286 * @param int|post object Optional. Post to check instead of the current post. @since 2.7.0
mp-wp_genesis 287 * @return bool True if the current post is in any of the given categories.
mp-wp_genesis 288 */
mp-wp_genesis 289 function in_category( $category, $_post = null ) {
mp-wp_genesis 290 if ( empty( $category ) )
mp-wp_genesis 291 return false;
mp-wp_genesis 292
mp-wp_genesis 293 if ( $_post ) {
mp-wp_genesis 294 $_post = get_post( $_post );
mp-wp_genesis 295 } else {
mp-wp_genesis 296 $_post =& $GLOBALS['post'];
mp-wp_genesis 297 }
mp-wp_genesis 298
mp-wp_genesis 299 if ( !$_post )
mp-wp_genesis 300 return false;
mp-wp_genesis 301
mp-wp_genesis 302 $r = is_object_in_term( $_post->ID, 'category', $category );
mp-wp_genesis 303 if ( is_wp_error( $r ) )
mp-wp_genesis 304 return false;
mp-wp_genesis 305 return $r;
mp-wp_genesis 306 }
mp-wp_genesis 307
mp-wp_genesis 308 /**
mp-wp_genesis 309 * Display the category list for the post.
mp-wp_genesis 310 *
mp-wp_genesis 311 * @since 0.71
mp-wp_genesis 312 *
mp-wp_genesis 313 * @param string $separator Optional, default is empty string. Separator for between the categories.
mp-wp_genesis 314 * @param string $parents Optional. How to display the parents.
mp-wp_genesis 315 * @param int $post_id Optional. Post ID to retrieve categories.
mp-wp_genesis 316 */
mp-wp_genesis 317 function the_category( $separator = '', $parents='', $post_id = false ) {
mp-wp_genesis 318 echo get_the_category_list( $separator, $parents, $post_id );
mp-wp_genesis 319 }
mp-wp_genesis 320
mp-wp_genesis 321 /**
mp-wp_genesis 322 * Retrieve category description.
mp-wp_genesis 323 *
mp-wp_genesis 324 * @since 1.0.0
mp-wp_genesis 325 *
mp-wp_genesis 326 * @param int $category Optional. Category ID. Will use global category ID by default.
mp-wp_genesis 327 * @return string Category description, available.
mp-wp_genesis 328 */
mp-wp_genesis 329 function category_description( $category = 0 ) {
mp-wp_genesis 330 global $cat;
mp-wp_genesis 331 if ( !$category )
mp-wp_genesis 332 $category = $cat;
mp-wp_genesis 333
mp-wp_genesis 334 return get_term_field( 'description', $category, 'category' );
mp-wp_genesis 335 }
mp-wp_genesis 336
mp-wp_genesis 337 /**
mp-wp_genesis 338 * Display or retrieve the HTML dropdown list of categories.
mp-wp_genesis 339 *
mp-wp_genesis 340 * The list of arguments is below:
mp-wp_genesis 341 * 'show_option_all' (string) - Text to display for showing all categories.
mp-wp_genesis 342 * 'show_option_none' (string) - Text to display for showing no categories.
mp-wp_genesis 343 * 'orderby' (string) default is 'ID' - What column to use for ordering the
mp-wp_genesis 344 * categories.
mp-wp_genesis 345 * 'order' (string) default is 'ASC' - What direction to order categories.
mp-wp_genesis 346 * 'show_last_update' (bool|int) default is 0 - See {@link get_categories()}
mp-wp_genesis 347 * 'show_count' (bool|int) default is 0 - Whether to show how many posts are
mp-wp_genesis 348 * in the category.
mp-wp_genesis 349 * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that
mp-wp_genesis 350 * don't have any posts attached to them.
mp-wp_genesis 351 * 'child_of' (int) default is 0 - See {@link get_categories()}.
mp-wp_genesis 352 * 'exclude' (string) - See {@link get_categories()}.
mp-wp_genesis 353 * 'echo' (bool|int) default is 1 - Whether to display or retrieve content.
mp-wp_genesis 354 * 'depth' (int) - The max depth.
mp-wp_genesis 355 * 'tab_index' (int) - Tab index for select element.
mp-wp_genesis 356 * 'name' (string) - The name attribute value for selected element.
mp-wp_genesis 357 * 'class' (string) - The class attribute value for selected element.
mp-wp_genesis 358 * 'selected' (int) - Which category ID is selected.
mp-wp_genesis 359 *
mp-wp_genesis 360 * The 'hierarchical' argument, which is disabled by default, will override the
mp-wp_genesis 361 * depth argument, unless it is true. When the argument is false, it will
mp-wp_genesis 362 * display all of the categories. When it is enabled it will use the value in
mp-wp_genesis 363 * the 'depth' argument.
mp-wp_genesis 364 *
mp-wp_genesis 365 * @since 2.1.0
mp-wp_genesis 366 *
mp-wp_genesis 367 * @param string|array $args Optional. Override default arguments.
mp-wp_genesis 368 * @return string HTML content only if 'echo' argument is 0.
mp-wp_genesis 369 */
mp-wp_genesis 370 function wp_dropdown_categories( $args = '' ) {
mp-wp_genesis 371 $defaults = array(
mp-wp_genesis 372 'show_option_all' => '', 'show_option_none' => '',
mp-wp_genesis 373 'orderby' => 'ID', 'order' => 'ASC',
mp-wp_genesis 374 'show_last_update' => 0, 'show_count' => 0,
mp-wp_genesis 375 'hide_empty' => 1, 'child_of' => 0,
mp-wp_genesis 376 'exclude' => '', 'echo' => 1,
mp-wp_genesis 377 'selected' => 0, 'hierarchical' => 0,
mp-wp_genesis 378 'name' => 'cat', 'class' => 'postform',
mp-wp_genesis 379 'depth' => 0, 'tab_index' => 0
mp-wp_genesis 380 );
mp-wp_genesis 381
mp-wp_genesis 382 $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
mp-wp_genesis 383
mp-wp_genesis 384 $r = wp_parse_args( $args, $defaults );
mp-wp_genesis 385 $r['include_last_update_time'] = $r['show_last_update'];
mp-wp_genesis 386 extract( $r );
mp-wp_genesis 387
mp-wp_genesis 388 $tab_index_attribute = '';
mp-wp_genesis 389 if ( (int) $tab_index > 0 )
mp-wp_genesis 390 $tab_index_attribute = " tabindex=\"$tab_index\"";
mp-wp_genesis 391
mp-wp_genesis 392 $categories = get_categories( $r );
mp-wp_genesis 393
mp-wp_genesis 394 $output = '';
mp-wp_genesis 395 if ( ! empty( $categories ) ) {
mp-wp_genesis 396 $output = "<select name='$name' id='$name' class='$class' $tab_index_attribute>\n";
mp-wp_genesis 397
mp-wp_genesis 398 if ( $show_option_all ) {
mp-wp_genesis 399 $show_option_all = apply_filters( 'list_cats', $show_option_all );
mp-wp_genesis 400 $output .= "\t<option value='0'>$show_option_all</option>\n";
mp-wp_genesis 401 }
mp-wp_genesis 402
mp-wp_genesis 403 if ( $show_option_none ) {
mp-wp_genesis 404 $show_option_none = apply_filters( 'list_cats', $show_option_none );
mp-wp_genesis 405 $output .= "\t<option value='-1'>$show_option_none</option>\n";
mp-wp_genesis 406 }
mp-wp_genesis 407
mp-wp_genesis 408 if ( $hierarchical )
mp-wp_genesis 409 $depth = $r['depth']; // Walk the full depth.
mp-wp_genesis 410 else
mp-wp_genesis 411 $depth = -1; // Flat.
mp-wp_genesis 412
mp-wp_genesis 413 $output .= walk_category_dropdown_tree( $categories, $depth, $r );
mp-wp_genesis 414 $output .= "</select>\n";
mp-wp_genesis 415 }
mp-wp_genesis 416
mp-wp_genesis 417 $output = apply_filters( 'wp_dropdown_cats', $output );
mp-wp_genesis 418
mp-wp_genesis 419 if ( $echo )
mp-wp_genesis 420 echo $output;
mp-wp_genesis 421
mp-wp_genesis 422 return $output;
mp-wp_genesis 423 }
mp-wp_genesis 424
mp-wp_genesis 425 /**
mp-wp_genesis 426 * Display or retrieve the HTML list of categories.
mp-wp_genesis 427 *
mp-wp_genesis 428 * The list of arguments is below:
mp-wp_genesis 429 * 'show_option_all' (string) - Text to display for showing all categories.
mp-wp_genesis 430 * 'orderby' (string) default is 'ID' - What column to use for ordering the
mp-wp_genesis 431 * categories.
mp-wp_genesis 432 * 'order' (string) default is 'ASC' - What direction to order categories.
mp-wp_genesis 433 * 'show_last_update' (bool|int) default is 0 - See {@link
mp-wp_genesis 434 * walk_category_dropdown_tree()}
mp-wp_genesis 435 * 'show_count' (bool|int) default is 0 - Whether to show how many posts are
mp-wp_genesis 436 * in the category.
mp-wp_genesis 437 * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that
mp-wp_genesis 438 * don't have any posts attached to them.
mp-wp_genesis 439 * 'use_desc_for_title' (bool|int) default is 1 - Whether to use the
mp-wp_genesis 440 * description instead of the category title.
mp-wp_genesis 441 * 'feed' - See {@link get_categories()}.
mp-wp_genesis 442 * 'feed_type' - See {@link get_categories()}.
mp-wp_genesis 443 * 'feed_image' - See {@link get_categories()}.
mp-wp_genesis 444 * 'child_of' (int) default is 0 - See {@link get_categories()}.
mp-wp_genesis 445 * 'exclude' (string) - See {@link get_categories()}.
mp-wp_genesis 446 * 'echo' (bool|int) default is 1 - Whether to display or retrieve content.
mp-wp_genesis 447 * 'current_category' (int) - See {@link get_categories()}.
mp-wp_genesis 448 * 'hierarchical' (bool) - See {@link get_categories()}.
mp-wp_genesis 449 * 'title_li' (string) - See {@link get_categories()}.
mp-wp_genesis 450 * 'depth' (int) - The max depth.
mp-wp_genesis 451 *
mp-wp_genesis 452 * @since 2.1.0
mp-wp_genesis 453 *
mp-wp_genesis 454 * @param string|array $args Optional. Override default arguments.
mp-wp_genesis 455 * @return string HTML content only if 'echo' argument is 0.
mp-wp_genesis 456 */
mp-wp_genesis 457 function wp_list_categories( $args = '' ) {
mp-wp_genesis 458 $defaults = array(
mp-wp_genesis 459 'show_option_all' => '', 'orderby' => 'name',
mp-wp_genesis 460 'order' => 'ASC', 'show_last_update' => 0,
mp-wp_genesis 461 'style' => 'list', 'show_count' => 0,
mp-wp_genesis 462 'hide_empty' => 1, 'use_desc_for_title' => 1,
mp-wp_genesis 463 'child_of' => 0, 'feed' => '', 'feed_type' => '',
mp-wp_genesis 464 'feed_image' => '', 'exclude' => '', 'current_category' => 0,
mp-wp_genesis 465 'hierarchical' => true, 'title_li' => __( 'Categories' ),
mp-wp_genesis 466 'echo' => 1, 'depth' => 0
mp-wp_genesis 467 );
mp-wp_genesis 468
mp-wp_genesis 469 $r = wp_parse_args( $args, $defaults );
mp-wp_genesis 470
mp-wp_genesis 471 if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
mp-wp_genesis 472 $r['pad_counts'] = true;
mp-wp_genesis 473 }
mp-wp_genesis 474
mp-wp_genesis 475 if ( isset( $r['show_date'] ) ) {
mp-wp_genesis 476 $r['include_last_update_time'] = $r['show_date'];
mp-wp_genesis 477 }
mp-wp_genesis 478
mp-wp_genesis 479 extract( $r );
mp-wp_genesis 480
mp-wp_genesis 481 $categories = get_categories( $r );
mp-wp_genesis 482
mp-wp_genesis 483 $output = '';
mp-wp_genesis 484 if ( $title_li && 'list' == $style )
mp-wp_genesis 485 $output = '<li class="categories">' . $r['title_li'] . '<ul>';
mp-wp_genesis 486
mp-wp_genesis 487 if ( empty( $categories ) ) {
mp-wp_genesis 488 if ( 'list' == $style )
mp-wp_genesis 489 $output .= '<li>' . __( "No categories" ) . '</li>';
mp-wp_genesis 490 else
mp-wp_genesis 491 $output .= __( "No categories" );
mp-wp_genesis 492 } else {
mp-wp_genesis 493 global $wp_query;
mp-wp_genesis 494
mp-wp_genesis 495 if( !empty( $show_option_all ) )
mp-wp_genesis 496 if ( 'list' == $style )
mp-wp_genesis 497 $output .= '<li><a href="' . get_bloginfo( 'url' ) . '">' . $show_option_all . '</a></li>';
mp-wp_genesis 498 else
mp-wp_genesis 499 $output .= '<a href="' . get_bloginfo( 'url' ) . '">' . $show_option_all . '</a>';
mp-wp_genesis 500
mp-wp_genesis 501 if ( empty( $r['current_category'] ) && is_category() )
mp-wp_genesis 502 $r['current_category'] = $wp_query->get_queried_object_id();
mp-wp_genesis 503
mp-wp_genesis 504 if ( $hierarchical )
mp-wp_genesis 505 $depth = $r['depth'];
mp-wp_genesis 506 else
mp-wp_genesis 507 $depth = -1; // Flat.
mp-wp_genesis 508
mp-wp_genesis 509 $output .= walk_category_tree( $categories, $depth, $r );
mp-wp_genesis 510 }
mp-wp_genesis 511
mp-wp_genesis 512 if ( $title_li && 'list' == $style )
mp-wp_genesis 513 $output .= '</ul></li>';
mp-wp_genesis 514
mp-wp_genesis 515 $output = apply_filters( 'wp_list_categories', $output );
mp-wp_genesis 516
mp-wp_genesis 517 if ( $echo )
mp-wp_genesis 518 echo $output;
mp-wp_genesis 519 else
mp-wp_genesis 520 return $output;
mp-wp_genesis 521 }
mp-wp_genesis 522
mp-wp_genesis 523 /**
mp-wp_genesis 524 * Display tag cloud.
mp-wp_genesis 525 *
mp-wp_genesis 526 * The text size is set by the 'smallest' and 'largest' arguments, which will
mp-wp_genesis 527 * use the 'unit' argument value for the CSS text size unit. The 'format'
mp-wp_genesis 528 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
mp-wp_genesis 529 * 'format' argument will separate tags with spaces. The list value for the
mp-wp_genesis 530 * 'format' argument will format the tags in a UL HTML list. The array value for
mp-wp_genesis 531 * the 'format' argument will return in PHP array type format.
mp-wp_genesis 532 *
mp-wp_genesis 533 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
mp-wp_genesis 534 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'.
mp-wp_genesis 535 *
mp-wp_genesis 536 * The 'number' argument is how many tags to return. By default, the limit will
mp-wp_genesis 537 * be to return the top 45 tags in the tag cloud list.
mp-wp_genesis 538 *
mp-wp_genesis 539 * The 'topic_count_text_callback' argument is a function, which, given the count
mp-wp_genesis 540 * of the posts with that tag, returns a text for the tooltip of the tag link.
mp-wp_genesis 541 * @see default_topic_count_text
mp-wp_genesis 542 *
mp-wp_genesis 543 * The 'exclude' and 'include' arguments are used for the {@link get_tags()}
mp-wp_genesis 544 * function. Only one should be used, because only one will be used and the
mp-wp_genesis 545 * other ignored, if they are both set.
mp-wp_genesis 546 *
mp-wp_genesis 547 * @since 2.3.0
mp-wp_genesis 548 *
mp-wp_genesis 549 * @param array|string $args Optional. Override default arguments.
mp-wp_genesis 550 * @return array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument.
mp-wp_genesis 551 */
mp-wp_genesis 552 function wp_tag_cloud( $args = '' ) {
mp-wp_genesis 553 $defaults = array(
mp-wp_genesis 554 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
mp-wp_genesis 555 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
mp-wp_genesis 556 'exclude' => '', 'include' => '', 'link' => 'view'
mp-wp_genesis 557 );
mp-wp_genesis 558 $args = wp_parse_args( $args, $defaults );
mp-wp_genesis 559
mp-wp_genesis 560 $tags = get_tags( array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
mp-wp_genesis 561
mp-wp_genesis 562 if ( empty( $tags ) )
mp-wp_genesis 563 return;
mp-wp_genesis 564
mp-wp_genesis 565 foreach ( $tags as $key => $tag ) {
mp-wp_genesis 566 if ( 'edit' == $args['link'] )
mp-wp_genesis 567 $link = get_edit_tag_link( $tag->term_id );
mp-wp_genesis 568 else
mp-wp_genesis 569 $link = get_tag_link( $tag->term_id );
mp-wp_genesis 570 if ( is_wp_error( $link ) )
mp-wp_genesis 571 return false;
mp-wp_genesis 572
mp-wp_genesis 573 $tags[ $key ]->link = $link;
mp-wp_genesis 574 $tags[ $key ]->id = $tag->term_id;
mp-wp_genesis 575 }
mp-wp_genesis 576
mp-wp_genesis 577 $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
mp-wp_genesis 578
mp-wp_genesis 579 $return = apply_filters( 'wp_tag_cloud', $return, $args );
mp-wp_genesis 580
mp-wp_genesis 581 if ( 'array' == $args['format'] )
mp-wp_genesis 582 return $return;
mp-wp_genesis 583
mp-wp_genesis 584 echo $return;
mp-wp_genesis 585 }
mp-wp_genesis 586
mp-wp_genesis 587 /**
mp-wp_genesis 588 * Default text for tooltip for tag links
mp-wp_genesis 589 *
mp-wp_genesis 590 * @param integer $count number of posts with that tag
mp-wp_genesis 591 * @return string text for the tooltip of a tag link.
mp-wp_genesis 592 */
mp-wp_genesis 593 function default_topic_count_text( $count ) {
mp-wp_genesis 594 return sprintf( __ngettext('%s topic', '%s topics', $count), number_format_i18n( $count ) );
mp-wp_genesis 595 }
mp-wp_genesis 596
mp-wp_genesis 597 /**
mp-wp_genesis 598 * Generates a tag cloud (heatmap) from provided data.
mp-wp_genesis 599 *
mp-wp_genesis 600 * The text size is set by the 'smallest' and 'largest' arguments, which will
mp-wp_genesis 601 * use the 'unit' argument value for the CSS text size unit. The 'format'
mp-wp_genesis 602 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
mp-wp_genesis 603 * 'format' argument will separate tags with spaces. The list value for the
mp-wp_genesis 604 * 'format' argument will format the tags in a UL HTML list. The array value for
mp-wp_genesis 605 * the 'format' argument will return in PHP array type format.
mp-wp_genesis 606 *
mp-wp_genesis 607 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
mp-wp_genesis 608 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or
mp-wp_genesis 609 * 'RAND'.
mp-wp_genesis 610 *
mp-wp_genesis 611 * The 'number' argument is how many tags to return. By default, the limit will
mp-wp_genesis 612 * be to return the entire tag cloud list.
mp-wp_genesis 613 *
mp-wp_genesis 614 * The 'topic_count_text_callback' argument is a function, which given the count
mp-wp_genesis 615 * of the posts with that tag returns a text for the tooltip of the tag link.
mp-wp_genesis 616 * @see default_topic_count_text
mp-wp_genesis 617 *
mp-wp_genesis 618 *
mp-wp_genesis 619 * @todo Complete functionality.
mp-wp_genesis 620 * @since 2.3.0
mp-wp_genesis 621 *
mp-wp_genesis 622 * @param array $tags List of tags.
mp-wp_genesis 623 * @param string|array $args Optional, override default arguments.
mp-wp_genesis 624 * @return string
mp-wp_genesis 625 */
mp-wp_genesis 626 function wp_generate_tag_cloud( $tags, $args = '' ) {
mp-wp_genesis 627 global $wp_rewrite;
mp-wp_genesis 628 $defaults = array(
mp-wp_genesis 629 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
mp-wp_genesis 630 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
mp-wp_genesis 631 'topic_count_text_callback' => 'default_topic_count_text',
mp-wp_genesis 632 );
mp-wp_genesis 633
mp-wp_genesis 634 if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
mp-wp_genesis 635 $body = 'return sprintf (
mp-wp_genesis 636 __ngettext('.var_export($args['single_text'], true).', '.var_export($args['multiple_text'], true).', $count),
mp-wp_genesis 637 number_format_i18n( $count ));';
mp-wp_genesis 638 $args['topic_count_text_callback'] = create_function('$count', $body);
mp-wp_genesis 639 }
mp-wp_genesis 640
mp-wp_genesis 641 $args = wp_parse_args( $args, $defaults );
mp-wp_genesis 642
mp-wp_genesis 643 extract( $args );
mp-wp_genesis 644
mp-wp_genesis 645 if ( empty( $tags ) )
mp-wp_genesis 646 return;
mp-wp_genesis 647
mp-wp_genesis 648 // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
mp-wp_genesis 649 if ( 'name' == $orderby )
mp-wp_genesis 650 uasort( $tags, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);') );
mp-wp_genesis 651 else
mp-wp_genesis 652 uasort( $tags, create_function('$a, $b', 'return ($a->count < $b->count);') );
mp-wp_genesis 653
mp-wp_genesis 654 if ( 'DESC' == $order )
mp-wp_genesis 655 $tags = array_reverse( $tags, true );
mp-wp_genesis 656 elseif ( 'RAND' == $order ) {
mp-wp_genesis 657 $keys = array_rand( $tags, count( $tags ) );
mp-wp_genesis 658 foreach ( $keys as $key )
mp-wp_genesis 659 $temp[$key] = $tags[$key];
mp-wp_genesis 660 $tags = $temp;
mp-wp_genesis 661 unset( $temp );
mp-wp_genesis 662 }
mp-wp_genesis 663
mp-wp_genesis 664 if ( $number > 0 )
mp-wp_genesis 665 $tags = array_slice($tags, 0, $number);
mp-wp_genesis 666
mp-wp_genesis 667 $counts = array();
mp-wp_genesis 668 foreach ( (array) $tags as $key => $tag )
mp-wp_genesis 669 $counts[ $key ] = $tag->count;
mp-wp_genesis 670
mp-wp_genesis 671 $min_count = min( $counts );
mp-wp_genesis 672 $spread = max( $counts ) - $min_count;
mp-wp_genesis 673 if ( $spread <= 0 )
mp-wp_genesis 674 $spread = 1;
mp-wp_genesis 675 $font_spread = $largest - $smallest;
mp-wp_genesis 676 if ( $font_spread < 0 )
mp-wp_genesis 677 $font_spread = 1;
mp-wp_genesis 678 $font_step = $font_spread / $spread;
mp-wp_genesis 679
mp-wp_genesis 680 $a = array();
mp-wp_genesis 681
mp-wp_genesis 682 $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
mp-wp_genesis 683
mp-wp_genesis 684 foreach ( $tags as $key => $tag ) {
mp-wp_genesis 685 $count = $counts[ $key ];
mp-wp_genesis 686 $tag_link = '#' != $tag->link ? clean_url( $tag->link ) : '#';
mp-wp_genesis 687 $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
mp-wp_genesis 688 $tag_name = $tags[ $key ]->name;
mp-wp_genesis 689 $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . attribute_escape( $topic_count_text_callback( $count ) ) . "'$rel style='font-size: " .
mp-wp_genesis 690 ( $smallest + ( ( $count - $min_count ) * $font_step ) )
mp-wp_genesis 691 . "$unit;'>$tag_name</a>";
mp-wp_genesis 692 }
mp-wp_genesis 693
mp-wp_genesis 694 switch ( $format ) :
mp-wp_genesis 695 case 'array' :
mp-wp_genesis 696 $return =& $a;
mp-wp_genesis 697 break;
mp-wp_genesis 698 case 'list' :
mp-wp_genesis 699 $return = "<ul class='wp-tag-cloud'>\n\t<li>";
mp-wp_genesis 700 $return .= join( "</li>\n\t<li>", $a );
mp-wp_genesis 701 $return .= "</li>\n</ul>\n";
mp-wp_genesis 702 break;
mp-wp_genesis 703 default :
mp-wp_genesis 704 $return = join( "\n", $a );
mp-wp_genesis 705 break;
mp-wp_genesis 706 endswitch;
mp-wp_genesis 707
mp-wp_genesis 708 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
mp-wp_genesis 709 }
mp-wp_genesis 710
mp-wp_genesis 711 //
mp-wp_genesis 712 // Helper functions
mp-wp_genesis 713 //
mp-wp_genesis 714
mp-wp_genesis 715 /**
mp-wp_genesis 716 * Retrieve HTML list content for category list.
mp-wp_genesis 717 *
mp-wp_genesis 718 * @uses Walker_Category to create HTML list content.
mp-wp_genesis 719 * @since 2.1.0
mp-wp_genesis 720 * @see Walker_Category::walk() for parameters and return description.
mp-wp_genesis 721 */
mp-wp_genesis 722 function walk_category_tree() {
mp-wp_genesis 723 $walker = new Walker_Category;
mp-wp_genesis 724 $args = func_get_args();
mp-wp_genesis 725 return call_user_func_array(array( &$walker, 'walk' ), $args );
mp-wp_genesis 726 }
mp-wp_genesis 727
mp-wp_genesis 728 /**
mp-wp_genesis 729 * Retrieve HTML dropdown (select) content for category list.
mp-wp_genesis 730 *
mp-wp_genesis 731 * @uses Walker_CategoryDropdown to create HTML dropdown content.
mp-wp_genesis 732 * @since 2.1.0
mp-wp_genesis 733 * @see Walker_CategoryDropdown::walk() for parameters and return description.
mp-wp_genesis 734 */
mp-wp_genesis 735 function walk_category_dropdown_tree() {
mp-wp_genesis 736 $walker = new Walker_CategoryDropdown;
mp-wp_genesis 737 $args = func_get_args();
mp-wp_genesis 738 return call_user_func_array(array( &$walker, 'walk' ), $args );
mp-wp_genesis 739 }
mp-wp_genesis 740
mp-wp_genesis 741 //
mp-wp_genesis 742 // Tags
mp-wp_genesis 743 //
mp-wp_genesis 744
mp-wp_genesis 745 /**
mp-wp_genesis 746 * Retrieve the link to the tag.
mp-wp_genesis 747 *
mp-wp_genesis 748 * @since 2.3.0
mp-wp_genesis 749 * @uses apply_filters() Calls 'tag_link' with tag link and tag ID as parameters.
mp-wp_genesis 750 *
mp-wp_genesis 751 * @param int $tag_id Tag (term) ID.
mp-wp_genesis 752 * @return string
mp-wp_genesis 753 */
mp-wp_genesis 754 function get_tag_link( $tag_id ) {
mp-wp_genesis 755 global $wp_rewrite;
mp-wp_genesis 756 $taglink = $wp_rewrite->get_tag_permastruct();
mp-wp_genesis 757
mp-wp_genesis 758 $tag = &get_term( $tag_id, 'post_tag' );
mp-wp_genesis 759 if ( is_wp_error( $tag ) )
mp-wp_genesis 760 return $tag;
mp-wp_genesis 761 $slug = $tag->slug;
mp-wp_genesis 762
mp-wp_genesis 763 if ( empty( $taglink ) ) {
mp-wp_genesis 764 $file = get_option( 'home' ) . '/';
mp-wp_genesis 765 $taglink = $file . '?tag=' . $slug;
mp-wp_genesis 766 } else {
mp-wp_genesis 767 $taglink = str_replace( '%tag%', $slug, $taglink );
mp-wp_genesis 768 $taglink = get_option( 'home' ) . user_trailingslashit( $taglink, 'category' );
mp-wp_genesis 769 }
mp-wp_genesis 770 return apply_filters( 'tag_link', $taglink, $tag_id );
mp-wp_genesis 771 }
mp-wp_genesis 772
mp-wp_genesis 773 /**
mp-wp_genesis 774 * Retrieve the tags for a post.
mp-wp_genesis 775 *
mp-wp_genesis 776 * @since 2.3.0
mp-wp_genesis 777 * @uses apply_filters() Calls 'get_the_tags' filter on the list of post tags.
mp-wp_genesis 778 *
mp-wp_genesis 779 * @param int $id Post ID.
mp-wp_genesis 780 * @return array
mp-wp_genesis 781 */
mp-wp_genesis 782 function get_the_tags( $id = 0 ) {
mp-wp_genesis 783 return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );
mp-wp_genesis 784 }
mp-wp_genesis 785
mp-wp_genesis 786 /**
mp-wp_genesis 787 * Retrieve the tags for a post formatted as a string.
mp-wp_genesis 788 *
mp-wp_genesis 789 * @since 2.3.0
mp-wp_genesis 790 * @uses apply_filters() Calls 'the_tags' filter on string list of tags.
mp-wp_genesis 791 *
mp-wp_genesis 792 * @param string $before Optional. Before tags.
mp-wp_genesis 793 * @param string $sep Optional. Between tags.
mp-wp_genesis 794 * @param string $after Optional. After tags.
mp-wp_genesis 795 * @return string
mp-wp_genesis 796 */
mp-wp_genesis 797 function get_the_tag_list( $before = '', $sep = '', $after = '' ) {
mp-wp_genesis 798 return apply_filters( 'the_tags', get_the_term_list( 0, 'post_tag', $before, $sep, $after ) );
mp-wp_genesis 799 }
mp-wp_genesis 800
mp-wp_genesis 801 /**
mp-wp_genesis 802 * Retrieve the tags for a post.
mp-wp_genesis 803 *
mp-wp_genesis 804 * @since 2.3.0
mp-wp_genesis 805 *
mp-wp_genesis 806 * @param string $before Optional. Before list.
mp-wp_genesis 807 * @param string $sep Optional. Separate items using this.
mp-wp_genesis 808 * @param string $after Optional. After list.
mp-wp_genesis 809 * @return string
mp-wp_genesis 810 */
mp-wp_genesis 811 function the_tags( $before = 'Tags: ', $sep = ', ', $after = '' ) {
mp-wp_genesis 812 return the_terms( 0, 'post_tag', $before, $sep, $after );
mp-wp_genesis 813 }
mp-wp_genesis 814
mp-wp_genesis 815 /**
mp-wp_genesis 816 * Retrieve the terms of the taxonomy that are attached to the post.
mp-wp_genesis 817 *
mp-wp_genesis 818 * This function can only be used within the loop.
mp-wp_genesis 819 *
mp-wp_genesis 820 * @since 2.5.0
mp-wp_genesis 821 *
mp-wp_genesis 822 * @param int $id Post ID. Is not optional.
mp-wp_genesis 823 * @param string $taxonomy Taxonomy name.
mp-wp_genesis 824 * @return array|bool False on failure. Array of term objects on success.
mp-wp_genesis 825 */
mp-wp_genesis 826 function get_the_terms( $id = 0, $taxonomy ) {
mp-wp_genesis 827 global $post;
mp-wp_genesis 828
mp-wp_genesis 829 $id = (int) $id;
mp-wp_genesis 830
mp-wp_genesis 831 if ( ! $id && ! in_the_loop() )
mp-wp_genesis 832 return false; // in-the-loop function
mp-wp_genesis 833
mp-wp_genesis 834 if ( !$id )
mp-wp_genesis 835 $id = (int) $post->ID;
mp-wp_genesis 836
mp-wp_genesis 837 $terms = get_object_term_cache( $id, $taxonomy );
mp-wp_genesis 838 if ( false === $terms )
mp-wp_genesis 839 $terms = wp_get_object_terms( $id, $taxonomy );
mp-wp_genesis 840
mp-wp_genesis 841 if ( empty( $terms ) )
mp-wp_genesis 842 return false;
mp-wp_genesis 843
mp-wp_genesis 844 return $terms;
mp-wp_genesis 845 }
mp-wp_genesis 846
mp-wp_genesis 847 /**
mp-wp_genesis 848 * Retrieve terms as a list with specified format.
mp-wp_genesis 849 *
mp-wp_genesis 850 * @since 2.5.0
mp-wp_genesis 851 *
mp-wp_genesis 852 * @param int $id Term ID.
mp-wp_genesis 853 * @param string $taxonomy Taxonomy name.
mp-wp_genesis 854 * @param string $before Optional. Before list.
mp-wp_genesis 855 * @param string $sep Optional. Separate items using this.
mp-wp_genesis 856 * @param string $after Optional. After list.
mp-wp_genesis 857 * @return string
mp-wp_genesis 858 */
mp-wp_genesis 859 function get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '' ) {
mp-wp_genesis 860 $terms = get_the_terms( $id, $taxonomy );
mp-wp_genesis 861
mp-wp_genesis 862 if ( is_wp_error( $terms ) )
mp-wp_genesis 863 return $terms;
mp-wp_genesis 864
mp-wp_genesis 865 if ( empty( $terms ) )
mp-wp_genesis 866 return false;
mp-wp_genesis 867
mp-wp_genesis 868 foreach ( $terms as $term ) {
mp-wp_genesis 869 $link = get_term_link( $term, $taxonomy );
mp-wp_genesis 870 if ( is_wp_error( $link ) )
mp-wp_genesis 871 return $link;
mp-wp_genesis 872 $term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
mp-wp_genesis 873 }
mp-wp_genesis 874
mp-wp_genesis 875 $term_links = apply_filters( "term_links-$taxonomy", $term_links );
mp-wp_genesis 876
mp-wp_genesis 877 return $before . join( $sep, $term_links ) . $after;
mp-wp_genesis 878 }
mp-wp_genesis 879
mp-wp_genesis 880 /**
mp-wp_genesis 881 * Display the terms in a list.
mp-wp_genesis 882 *
mp-wp_genesis 883 * @since 2.5.0
mp-wp_genesis 884 *
mp-wp_genesis 885 * @param int $id Term ID.
mp-wp_genesis 886 * @param string $taxonomy Taxonomy name.
mp-wp_genesis 887 * @param string $before Optional. Before list.
mp-wp_genesis 888 * @param string $sep Optional. Separate items using this.
mp-wp_genesis 889 * @param string $after Optional. After list.
mp-wp_genesis 890 * @return null|bool False on WordPress error. Returns null when displaying.
mp-wp_genesis 891 */
mp-wp_genesis 892 function the_terms( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
mp-wp_genesis 893 $return = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
mp-wp_genesis 894 if ( is_wp_error( $return ) )
mp-wp_genesis 895 return false;
mp-wp_genesis 896 else
mp-wp_genesis 897 echo $return;
mp-wp_genesis 898 }
mp-wp_genesis 899
mp-wp_genesis 900 /**
mp-wp_genesis 901 * Check if the current post has any of given tags.
mp-wp_genesis 902 *
mp-wp_genesis 903 * The given tags are checked against the post's tags' term_ids, names and slugs.
mp-wp_genesis 904 * Tags given as integers will only be checked against the post's tags' term_ids.
mp-wp_genesis 905 * If no tags are given, determines if post has any tags.
mp-wp_genesis 906 *
mp-wp_genesis 907 * 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)
mp-wp_genesis 908 * Prior to v2.7, this function could only be used in the WordPress Loop.
mp-wp_genesis 909 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
mp-wp_genesis 910 *
mp-wp_genesis 911 * @since 2.6.0
mp-wp_genesis 912 *
mp-wp_genesis 913 * @uses is_object_in_term()
mp-wp_genesis 914 *
mp-wp_genesis 915 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
mp-wp_genesis 916 * @param int|post object Optional. Post to check instead of the current post. @since 2.7.0
mp-wp_genesis 917 * @return bool True if the current post has any of the the given tags (or any tag, if no tag specified).
mp-wp_genesis 918 */
mp-wp_genesis 919 function has_tag( $tag = '', $_post = null ) {
mp-wp_genesis 920 if ( $_post ) {
mp-wp_genesis 921 $_post = get_post( $_post );
mp-wp_genesis 922 } else {
mp-wp_genesis 923 $_post =& $GLOBALS['post'];
mp-wp_genesis 924 }
mp-wp_genesis 925
mp-wp_genesis 926 if ( !$_post )
mp-wp_genesis 927 return false;
mp-wp_genesis 928
mp-wp_genesis 929 $r = is_object_in_term( $_post->ID, 'post_tag', $tag );
mp-wp_genesis 930 if ( is_wp_error( $r ) )
mp-wp_genesis 931 return false;
mp-wp_genesis 932 return $r;
mp-wp_genesis 933 }
mp-wp_genesis 934
mp-wp_genesis 935 ?>