[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> category-template.php (source)

   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
  41   */
  42  function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
  43      $chain = '';
  44      $parent = &get_category( $id );
  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 )
  59          $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
  60      else
  61          $chain .= $name.$separator;
  62      return $chain;
  63  }
  64  
  65  /**
  66   * Retrieve post categories.
  67   *
  68   * @since 0.71
  69   * @uses $post
  70   *
  71   * @param int $id Optional, default to current post ID. The post ID.
  72   * @return array
  73   */
  74  function get_the_category( $id = false ) {
  75      $categories = get_the_terms( $id, 'category' );
  76      if ( ! $categories )
  77          $categories = array();
  78  
  79      $categories = array_values( $categories );
  80  
  81      foreach ( array_keys( $categories ) as $key ) {
  82          _make_cat_compat( $categories[$key] );
  83      }
  84  
  85      // Filter name is plural because we return alot of categories (possibly more than #13237) not just one
  86      return apply_filters( 'get_the_categories', $categories );
  87  }
  88  
  89  /**
  90   * Sort categories by name.
  91   *
  92   * Used by usort() as a callback, should not be used directly. Can actually be
  93   * used to sort any term object.
  94   *
  95   * @since 2.3.0
  96   * @access private
  97   *
  98   * @param object $a
  99   * @param object $b
 100   * @return int
 101   */
 102  function _usort_terms_by_name( $a, $b ) {
 103      return strcmp( $a->name, $b->name );
 104  }
 105  
 106  /**
 107   * Sort categories by ID.
 108   *
 109   * Used by usort() as a callback, should not be used directly. Can actually be
 110   * used to sort any term object.
 111   *
 112   * @since 2.3.0
 113   * @access private
 114   *
 115   * @param object $a
 116   * @param object $b
 117   * @return int
 118   */
 119  function _usort_terms_by_ID( $a, $b ) {
 120      if ( $a->term_id > $b->term_id )
 121          return 1;
 122      elseif ( $a->term_id < $b->term_id )
 123          return -1;
 124      else
 125          return 0;
 126  }
 127  
 128  /**
 129   * Retrieve category name based on category ID.
 130   *
 131   * @since 0.71
 132   *
 133   * @param int $cat_ID Category ID.
 134   * @return string Category name.
 135   */
 136  function get_the_category_by_ID( $cat_ID ) {
 137      $cat_ID = (int) $cat_ID;
 138      $category = &get_category( $cat_ID );
 139      if ( is_wp_error( $category ) )
 140          return $category;
 141      return $category->name;
 142  }
 143  
 144  /**
 145   * Retrieve category list in either HTML list or custom format.
 146   *
 147   * @since 1.5.1
 148   *
 149   * @param string $separator Optional, default is empty string. Separator for between the categories.
 150   * @param string $parents Optional. How to display the parents.
 151   * @param int $post_id Optional. Post ID to retrieve categories.
 152   * @return string
 153   */
 154  function get_the_category_list( $separator = '', $parents='', $post_id = false ) {
 155      global $wp_rewrite;
 156      if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) )
 157          return apply_filters( 'the_category', '', $separator, $parents );
 158  
 159      $categories = get_the_category( $post_id );
 160      if ( empty( $categories ) )
 161          return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
 162  
 163      $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
 164  
 165      $thelist = '';
 166      if ( '' == $separator ) {
 167          $thelist .= '<ul class="post-categories">';
 168          foreach ( $categories as $category ) {
 169              $thelist .= "\n\t<li>";
 170              switch ( strtolower( $parents ) ) {
 171                  case 'multiple':
 172                      if ( $category->parent )
 173                          $thelist .= get_category_parents( $category->parent, true, $separator );
 174                      $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
 175                      break;
 176                  case 'single':
 177                      $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
 178                      if ( $category->parent )
 179                          $thelist .= get_category_parents( $category->parent, false, $separator );
 180                      $thelist .= $category->name.'</a></li>';
 181                      break;
 182                  case '':
 183                  default:
 184                      $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
 185              }
 186          }
 187          $thelist .= '</ul>';
 188      } else {
 189          $i = 0;
 190          foreach ( $categories as $category ) {
 191              if ( 0 < $i )
 192                  $thelist .= $separator;
 193              switch ( strtolower( $parents ) ) {
 194                  case 'multiple':
 195                      if ( $category->parent )
 196                          $thelist .= get_category_parents( $category->parent, true, $separator );
 197                      $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a>';
 198                      break;
 199                  case 'single':
 200                      $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
 201                      if ( $category->parent )
 202                          $thelist .= get_category_parents( $category->parent, false, $separator );
 203                      $thelist .= "$category->name</a>";
 204                      break;
 205                  case '':
 206                  default:
 207                      $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a>';
 208              }
 209              ++$i;
 210          }
 211      }
 212      return apply_filters( 'the_category', $thelist, $separator, $parents );
 213  }
 214  
 215  /**
 216   * Check if the current post in within any of the given categories.
 217   *
 218   * The given categories are checked against the post's categories' term_ids, names and slugs.
 219   * Categories given as integers will only be checked against the post's categories' term_ids.
 220   *
 221   * Prior to v2.5 of WordPress, category names were not supported.
 222   * Prior to v2.7, category slugs were not supported.
 223   * Prior to v2.7, only one category could be compared: in_category( $single_category ).
 224   * Prior to v2.7, this function could only be used in the WordPress Loop.
 225   * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
 226   *
 227   * @since 1.2.0
 228   *
 229   * @param int|string|array $category Category ID, name or slug, or array of said.
 230   * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
 231   * @return bool True if the current post is in any of the given categories.
 232   */
 233  function in_category( $category, $post = null ) {
 234      if ( empty( $category ) )
 235          return false;
 236  
 237      return has_term( $category, 'category', $post );
 238  }
 239  
 240  /**
 241   * Display the category list for the post.
 242   *
 243   * @since 0.71
 244   *
 245   * @param string $separator Optional, default is empty string. Separator for between the categories.
 246   * @param string $parents Optional. How to display the parents.
 247   * @param int $post_id Optional. Post ID to retrieve categories.
 248   */
 249  function the_category( $separator = '', $parents='', $post_id = false ) {
 250      echo get_the_category_list( $separator, $parents, $post_id );
 251  }
 252  
 253  /**
 254   * Retrieve category description.
 255   *
 256   * @since 1.0.0
 257   *
 258   * @param int $category Optional. Category ID. Will use global category ID by default.
 259   * @return string Category description, available.
 260   */
 261  function category_description( $category = 0 ) {
 262      return term_description( $category, 'category' );
 263  }
 264  
 265  /**
 266   * Display or retrieve the HTML dropdown list of categories.
 267   *
 268   * The list of arguments is below:
 269   *     'show_option_all' (string) - Text to display for showing all categories.
 270   *     'show_option_none' (string) - Text to display for showing no categories.
 271   *     'orderby' (string) default is 'ID' - What column to use for ordering the
 272   * categories.
 273   *     'order' (string) default is 'ASC' - What direction to order categories.
 274   *     'show_count' (bool|int) default is 0 - Whether to show how many posts are
 275   * in the category.
 276   *     'hide_empty' (bool|int) default is 1 - Whether to hide categories that
 277   * don't have any posts attached to them.
 278   *     'child_of' (int) default is 0 - See {@link get_categories()}.
 279   *     'exclude' (string) - See {@link get_categories()}.
 280   *     'echo' (bool|int) default is 1 - Whether to display or retrieve content.
 281   *     'depth' (int) - The max depth.
 282   *     'tab_index' (int) - Tab index for select element.
 283   *     'name' (string) - The name attribute value for select element.
 284   *     'id' (string) - The ID attribute value for select element. Defaults to name if omitted.
 285   *     'class' (string) - The class attribute value for select element.
 286   *     'selected' (int) - Which category ID is selected.
 287   *     'taxonomy' (string) - The name of the taxonomy to retrieve. Defaults to category.
 288   *
 289   * The 'hierarchical' argument, which is disabled by default, will override the
 290   * depth argument, unless it is true. When the argument is false, it will
 291   * display all of the categories. When it is enabled it will use the value in
 292   * the 'depth' argument.
 293   *
 294   * @since 2.1.0
 295   *
 296   * @param string|array $args Optional. Override default arguments.
 297   * @return string HTML content only if 'echo' argument is 0.
 298   */
 299  function wp_dropdown_categories( $args = '' ) {
 300      $defaults = array(
 301          'show_option_all' => '', 'show_option_none' => '',
 302          'orderby' => 'id', 'order' => 'ASC',
 303          'show_count' => 0,
 304          'hide_empty' => 1, 'child_of' => 0,
 305          'exclude' => '', 'echo' => 1,
 306          'selected' => 0, 'hierarchical' => 0,
 307          'name' => 'cat', 'id' => '',
 308          'class' => 'postform', 'depth' => 0,
 309          'tab_index' => 0, 'taxonomy' => 'category',
 310          'hide_if_empty' => false
 311      );
 312  
 313      $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
 314  
 315      // Back compat.
 316      if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
 317          _deprecated_argument( __FUNCTION__, '3.0', '' );
 318          $args['taxonomy'] = 'link_category';
 319      }
 320  
 321      $r = wp_parse_args( $args, $defaults );
 322  
 323      if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
 324          $r['pad_counts'] = true;
 325      }
 326  
 327      extract( $r );
 328  
 329      $tab_index_attribute = '';
 330      if ( (int) $tab_index > 0 )
 331          $tab_index_attribute = " tabindex=\"$tab_index\"";
 332  
 333      $categories = get_terms( $taxonomy, $r );
 334      $name = esc_attr( $name );
 335      $class = esc_attr( $class );
 336      $id = $id ? esc_attr( $id ) : $name;
 337  
 338      if ( ! $r['hide_if_empty'] || ! empty($categories) )
 339          $output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
 340      else
 341          $output = '';
 342  
 343      if ( empty($categories) && ! $r['hide_if_empty'] && !empty($show_option_none) ) {
 344          $show_option_none = apply_filters( 'list_cats', $show_option_none );
 345          $output .= "\t<option value='-1' selected='selected'>$show_option_none</option>\n";
 346      }
 347  
 348      if ( ! empty( $categories ) ) {
 349  
 350          if ( $show_option_all ) {
 351              $show_option_all = apply_filters( 'list_cats', $show_option_all );
 352              $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
 353              $output .= "\t<option value='0'$selected>$show_option_all</option>\n";
 354          }
 355  
 356          if ( $show_option_none ) {
 357              $show_option_none = apply_filters( 'list_cats', $show_option_none );
 358              $selected = ( '-1' === strval($r['selected']) ) ? " selected='selected'" : '';
 359              $output .= "\t<option value='-1'$selected>$show_option_none</option>\n";
 360          }
 361  
 362          if ( $hierarchical )
 363              $depth = $r['depth'];  // Walk the full depth.
 364          else
 365              $depth = -1; // Flat.
 366  
 367          $output .= walk_category_dropdown_tree( $categories, $depth, $r );
 368      }
 369  
 370      if ( ! $r['hide_if_empty'] || ! empty($categories) )
 371          $output .= "</select>\n";
 372  
 373      $output = apply_filters( 'wp_dropdown_cats', $output );
 374  
 375      if ( $echo )
 376          echo $output;
 377  
 378      return $output;
 379  }
 380  
 381  /**
 382   * Display or retrieve the HTML list of categories.
 383   *
 384   * The list of arguments is below:
 385   *     'show_option_all' (string) - Text to display for showing all categories.
 386   *     'orderby' (string) default is 'ID' - What column to use for ordering the
 387   * categories.
 388   *     'order' (string) default is 'ASC' - What direction to order categories.
 389   *     'show_count' (bool|int) default is 0 - Whether to show how many posts are
 390   * in the category.
 391   *     'hide_empty' (bool|int) default is 1 - Whether to hide categories that
 392   * don't have any posts attached to them.
 393   *     'use_desc_for_title' (bool|int) default is 1 - Whether to use the
 394   * description instead of the category title.
 395   *     'feed' - See {@link get_categories()}.
 396   *     'feed_type' - See {@link get_categories()}.
 397   *     'feed_image' - See {@link get_categories()}.
 398   *     'child_of' (int) default is 0 - See {@link get_categories()}.
 399   *     'exclude' (string) - See {@link get_categories()}.
 400   *     'exclude_tree' (string) - See {@link get_categories()}.
 401   *     'echo' (bool|int) default is 1 - Whether to display or retrieve content.
 402   *     'current_category' (int) - See {@link get_categories()}.
 403   *     'hierarchical' (bool) - See {@link get_categories()}.
 404   *     'title_li' (string) - See {@link get_categories()}.
 405   *     'depth' (int) - The max depth.
 406   *
 407   * @since 2.1.0
 408   *
 409   * @param string|array $args Optional. Override default arguments.
 410   * @return string HTML content only if 'echo' argument is 0.
 411   */
 412  function wp_list_categories( $args = '' ) {
 413      $defaults = array(
 414          'show_option_all' => '', 'show_option_none' => __('No categories'),
 415          'orderby' => 'name', 'order' => 'ASC',
 416          'style' => 'list',
 417          'show_count' => 0, 'hide_empty' => 1,
 418          'use_desc_for_title' => 1, 'child_of' => 0,
 419          'feed' => '', 'feed_type' => '',
 420          'feed_image' => '', 'exclude' => '',
 421          'exclude_tree' => '', 'current_category' => 0,
 422          'hierarchical' => true, 'title_li' => __( 'Categories' ),
 423          'echo' => 1, 'depth' => 0,
 424          'taxonomy' => 'category'
 425      );
 426  
 427      $r = wp_parse_args( $args, $defaults );
 428  
 429      if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
 430          $r['pad_counts'] = true;
 431  
 432      if ( true == $r['hierarchical'] ) {
 433          $r['exclude_tree'] = $r['exclude'];
 434          $r['exclude'] = '';
 435      }
 436  
 437      if ( !isset( $r['class'] ) )
 438          $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy'];
 439  
 440      extract( $r );
 441  
 442      if ( !taxonomy_exists($taxonomy) )
 443          return false;
 444  
 445      $categories = get_categories( $r );
 446  
 447      $output = '';
 448      if ( $title_li && 'list' == $style )
 449              $output = '<li class="' . esc_attr( $class ) . '">' . $title_li . '<ul>';
 450  
 451      if ( empty( $categories ) ) {
 452          if ( ! empty( $show_option_none ) ) {
 453              if ( 'list' == $style )
 454                  $output .= '<li>' . $show_option_none . '</li>';
 455              else
 456                  $output .= $show_option_none;
 457          }
 458      } else {
 459          if ( ! empty( $show_option_all ) ) {
 460              $posts_page = ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ) ? get_permalink( get_option( 'page_for_posts' ) ) : home_url( '/' );
 461              $posts_page = esc_url( $posts_page );
 462              if ( 'list' == $style )
 463                  $output .= "<li><a href='$posts_page'>$show_option_all</a></li>";
 464              else
 465                  $output .= "<a href='$posts_page'>$show_option_all</a>";
 466          }
 467  
 468          if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) {
 469              $current_term_object = get_queried_object();
 470              if ( $r['taxonomy'] == $current_term_object->taxonomy )
 471                  $r['current_category'] = get_queried_object_id();
 472          }
 473  
 474          if ( $hierarchical )
 475              $depth = $r['depth'];
 476          else
 477              $depth = -1; // Flat.
 478  
 479          $output .= walk_category_tree( $categories, $depth, $r );
 480      }
 481  
 482      if ( $title_li && 'list' == $style )
 483          $output .= '</ul></li>';
 484  
 485      $output = apply_filters( 'wp_list_categories', $output, $args );
 486  
 487      if ( $echo )
 488          echo $output;
 489      else
 490          return $output;
 491  }
 492  
 493  /**
 494   * Display tag cloud.
 495   *
 496   * The text size is set by the 'smallest' and 'largest' arguments, which will
 497   * use the 'unit' argument value for the CSS text size unit. The 'format'
 498   * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
 499   * 'format' argument will separate tags with spaces. The list value for the
 500   * 'format' argument will format the tags in a UL HTML list. The array value for
 501   * the 'format' argument will return in PHP array type format.
 502   *
 503   * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
 504   * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'.
 505   *
 506   * The 'number' argument is how many tags to return. By default, the limit will
 507   * be to return the top 45 tags in the tag cloud list.
 508   *
 509   * The 'topic_count_text_callback' argument is a function, which, given the count
 510   * of the posts  with that tag, returns a text for the tooltip of the tag link.
 511   *
 512   * The 'exclude' and 'include' arguments are used for the {@link get_tags()}
 513   * function. Only one should be used, because only one will be used and the
 514   * other ignored, if they are both set.
 515   *
 516   * @since 2.3.0
 517   *
 518   * @param array|string $args Optional. Override default arguments.
 519   * @return array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument.
 520   */
 521  function wp_tag_cloud( $args = '' ) {
 522      $defaults = array(
 523          'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
 524          'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
 525          'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true
 526      );
 527      $args = wp_parse_args( $args, $defaults );
 528  
 529      $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
 530  
 531      if ( empty( $tags ) || is_wp_error( $tags ) )
 532          return;
 533  
 534      foreach ( $tags as $key => $tag ) {
 535          if ( 'edit' == $args['link'] )
 536              $link = get_edit_tag_link( $tag->term_id, $tag->taxonomy );
 537          else
 538              $link = get_term_link( intval($tag->term_id), $tag->taxonomy );
 539          if ( is_wp_error( $link ) )
 540              return false;
 541  
 542          $tags[ $key ]->link = $link;
 543          $tags[ $key ]->id = $tag->term_id;
 544      }
 545  
 546      $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
 547  
 548      $return = apply_filters( 'wp_tag_cloud', $return, $args );
 549  
 550      if ( 'array' == $args['format'] || empty($args['echo']) )
 551          return $return;
 552  
 553      echo $return;
 554  }
 555  
 556  /**
 557   * Default text for tooltip for tag links
 558   *
 559   * @param integer $count number of posts with that tag
 560   * @return string text for the tooltip of a tag link.
 561   */
 562  function default_topic_count_text( $count ) {
 563      return sprintf( _n('%s topic', '%s topics', $count), number_format_i18n( $count ) );
 564  }
 565  
 566  /**
 567   * Default topic count scaling for tag links
 568   *
 569   * @param integer $count number of posts with that tag
 570   * @return integer scaled count
 571   */
 572  function default_topic_count_scale( $count ) {
 573      return round(log10($count + 1) * 100);
 574  }
 575  
 576  /**
 577   * Generates a tag cloud (heatmap) from provided data.
 578   *
 579   * The text size is set by the 'smallest' and 'largest' arguments, which will
 580   * use the 'unit' argument value for the CSS text size unit. The 'format'
 581   * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
 582   * 'format' argument will separate tags with spaces. The list value for the
 583   * 'format' argument will format the tags in a UL HTML list. The array value for
 584   * the 'format' argument will return in PHP array type format.
 585   *
 586   * The 'tag_cloud_sort' filter allows you to override the sorting.
 587   * Passed to the filter: $tags array and $args array, has to return the $tags array
 588   * after sorting it.
 589   *
 590   * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
 591   * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or
 592   * 'RAND'.
 593   *
 594   * The 'number' argument is how many tags to return. By default, the limit will
 595   * be to return the entire tag cloud list.
 596   *
 597   * The 'topic_count_text_callback' argument is a function, which given the count
 598   * of the posts  with that tag returns a text for the tooltip of the tag link.
 599   *
 600   * @todo Complete functionality.
 601   * @since 2.3.0
 602   *
 603   * @param array $tags List of tags.
 604   * @param string|array $args Optional, override default arguments.
 605   * @return string
 606   */
 607  function wp_generate_tag_cloud( $tags, $args = '' ) {
 608      $defaults = array(
 609          'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
 610          'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
 611          'topic_count_text_callback' => 'default_topic_count_text',
 612          'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
 613      );
 614  
 615      if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
 616          $body = 'return sprintf (
 617              _n(' . var_export($args['single_text'], true) . ', ' . var_export($args['multiple_text'], true) . ', $count),
 618              number_format_i18n( $count ));';
 619          $args['topic_count_text_callback'] = create_function('$count', $body);
 620      }
 621  
 622      $args = wp_parse_args( $args, $defaults );
 623      extract( $args );
 624  
 625      if ( empty( $tags ) )
 626          return;
 627  
 628      $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
 629      if ( $tags_sorted != $tags  ) { // the tags have been sorted by a plugin
 630          $tags = $tags_sorted;
 631          unset($tags_sorted);
 632      } else {
 633          if ( 'RAND' == $order ) {
 634              shuffle($tags);
 635          } else {
 636              // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
 637              if ( 'name' == $orderby )
 638                  uasort( $tags, '_wp_object_name_sort_cb' );
 639              else
 640                  uasort( $tags, '_wp_object_count_sort_cb' );
 641  
 642              if ( 'DESC' == $order )
 643                  $tags = array_reverse( $tags, true );
 644          }
 645      }
 646  
 647      if ( $number > 0 )
 648          $tags = array_slice($tags, 0, $number);
 649  
 650      $counts = array();
 651      $real_counts = array(); // For the alt tag
 652      foreach ( (array) $tags as $key => $tag ) {
 653          $real_counts[ $key ] = $tag->count;
 654          $counts[ $key ] = $topic_count_scale_callback($tag->count);
 655      }
 656  
 657      $min_count = min( $counts );
 658      $spread = max( $counts ) - $min_count;
 659      if ( $spread <= 0 )
 660          $spread = 1;
 661      $font_spread = $largest - $smallest;
 662      if ( $font_spread < 0 )
 663          $font_spread = 1;
 664      $font_step = $font_spread / $spread;
 665  
 666      $a = array();
 667  
 668      foreach ( $tags as $key => $tag ) {
 669          $count = $counts[ $key ];
 670          $real_count = $real_counts[ $key ];
 671          $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
 672          $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
 673          $tag_name = $tags[ $key ]->name;
 674          $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( call_user_func( $topic_count_text_callback, $real_count ) ) . "' style='font-size: " .
 675              str_replace( ',', '.', ( $smallest + ( ( $count - $min_count ) * $font_step ) ) )
 676              . "$unit;'>$tag_name</a>";
 677      }
 678  
 679      switch ( $format ) :
 680      case 'array' :
 681          $return =& $a;
 682          break;
 683      case 'list' :
 684          $return = "<ul class='wp-tag-cloud'>\n\t<li>";
 685          $return .= join( "</li>\n\t<li>", $a );
 686          $return .= "</li>\n</ul>\n";
 687          break;
 688      default :
 689          $return = join( $separator, $a );
 690          break;
 691      endswitch;
 692  
 693      if ( $filter )
 694          return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
 695      else
 696          return $return;
 697  }
 698  
 699  /**
 700   * Callback for comparing objects based on name
 701   *
 702   * @since 3.1.0
 703   * @access private
 704   */
 705  function _wp_object_name_sort_cb( $a, $b ) {
 706      return strnatcasecmp( $a->name, $b->name );
 707  }
 708  
 709  /**
 710   * Callback for comparing objects based on count
 711   *
 712   * @since 3.1.0
 713   * @access private
 714   */
 715  function _wp_object_count_sort_cb( $a, $b ) {
 716      return ( $a->count > $b->count );
 717  }
 718  
 719  //
 720  // Helper functions
 721  //
 722  
 723  /**
 724   * Retrieve HTML list content for category list.
 725   *
 726   * @uses Walker_Category to create HTML list content.
 727   * @since 2.1.0
 728   * @see Walker_Category::walk() for parameters and return description.
 729   */
 730  function walk_category_tree() {
 731      $args = func_get_args();
 732      // the user's options are the third parameter
 733      if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
 734          $walker = new Walker_Category;
 735      else
 736          $walker = $args[2]['walker'];
 737  
 738      return call_user_func_array(array( &$walker, 'walk' ), $args );
 739  }
 740  
 741  /**
 742   * Retrieve HTML dropdown (select) content for category list.
 743   *
 744   * @uses Walker_CategoryDropdown to create HTML dropdown content.
 745   * @since 2.1.0
 746   * @see Walker_CategoryDropdown::walk() for parameters and return description.
 747   */
 748  function walk_category_dropdown_tree() {
 749      $args = func_get_args();
 750      // the user's options are the third parameter
 751      if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
 752          $walker = new Walker_CategoryDropdown;
 753      else
 754          $walker = $args[2]['walker'];
 755  
 756      return call_user_func_array(array( &$walker, 'walk' ), $args );
 757  }
 758  
 759  /**
 760   * Create HTML list of categories.
 761   *
 762   * @package WordPress
 763   * @since 2.1.0
 764   * @uses Walker
 765   */
 766  class Walker_Category extends Walker {
 767      /**
 768       * @see Walker::$tree_type
 769       * @since 2.1.0
 770       * @var string
 771       */
 772      var $tree_type = 'category';
 773  
 774      /**
 775       * @see Walker::$db_fields
 776       * @since 2.1.0
 777       * @todo Decouple this
 778       * @var array
 779       */
 780      var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
 781  
 782      /**
 783       * @see Walker::start_lvl()
 784       * @since 2.1.0
 785       *
 786       * @param string $output Passed by reference. Used to append additional content.
 787       * @param int $depth Depth of category. Used for tab indentation.
 788       * @param array $args Will only append content if style argument value is 'list'.
 789       */
 790  	function start_lvl( &$output, $depth = 0, $args = array() ) {
 791          if ( 'list' != $args['style'] )
 792              return;
 793  
 794          $indent = str_repeat("\t", $depth);
 795          $output .= "$indent<ul class='children'>\n";
 796      }
 797  
 798      /**
 799       * @see Walker::end_lvl()
 800       * @since 2.1.0
 801       *
 802       * @param string $output Passed by reference. Used to append additional content.
 803       * @param int $depth Depth of category. Used for tab indentation.
 804       * @param array $args Will only append content if style argument value is 'list'.
 805       */
 806  	function end_lvl( &$output, $depth = 0, $args = array() ) {
 807          if ( 'list' != $args['style'] )
 808              return;
 809  
 810          $indent = str_repeat("\t", $depth);
 811          $output .= "$indent</ul>\n";
 812      }
 813  
 814      /**
 815       * @see Walker::start_el()
 816       * @since 2.1.0
 817       *
 818       * @param string $output Passed by reference. Used to append additional content.
 819       * @param object $category Category data object.
 820       * @param int $depth Depth of category in reference to parents.
 821       * @param array $args
 822       */
 823  	function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
 824          extract($args);
 825  
 826          $cat_name = esc_attr( $category->name );
 827          $cat_name = apply_filters( 'list_cats', $cat_name, $category );
 828          $link = '<a href="' . esc_url( get_term_link($category) ) . '" ';
 829          if ( $use_desc_for_title == 0 || empty($category->description) )
 830              $link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';
 831          else
 832              $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
 833          $link .= '>';
 834          $link .= $cat_name . '</a>';
 835  
 836          if ( !empty($feed_image) || !empty($feed) ) {
 837              $link .= ' ';
 838  
 839              if ( empty($feed_image) )
 840                  $link .= '(';
 841  
 842              $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) ) . '"';
 843  
 844              if ( empty($feed) ) {
 845                  $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
 846              } else {
 847                  $title = ' title="' . $feed . '"';
 848                  $alt = ' alt="' . $feed . '"';
 849                  $name = $feed;
 850                  $link .= $title;
 851              }
 852  
 853              $link .= '>';
 854  
 855              if ( empty($feed_image) )
 856                  $link .= $name;
 857              else
 858                  $link .= "<img src='$feed_image'$alt$title" . ' />';
 859  
 860              $link .= '</a>';
 861  
 862              if ( empty($feed_image) )
 863                  $link .= ')';
 864          }
 865  
 866          if ( !empty($show_count) )
 867              $link .= ' (' . intval($category->count) . ')';
 868  
 869          if ( 'list' == $args['style'] ) {
 870              $output .= "\t<li";
 871              $class = 'cat-item cat-item-' . $category->term_id;
 872              if ( !empty($current_category) ) {
 873                  $_current_category = get_term( $current_category, $category->taxonomy );
 874                  if ( $category->term_id == $current_category )
 875                      $class .=  ' current-cat';
 876                  elseif ( $category->term_id == $_current_category->parent )
 877                      $class .=  ' current-cat-parent';
 878              }
 879              $output .=  ' class="' . $class . '"';
 880              $output .= ">$link\n";
 881          } else {
 882              $output .= "\t$link<br />\n";
 883          }
 884      }
 885  
 886      /**
 887       * @see Walker::end_el()
 888       * @since 2.1.0
 889       *
 890       * @param string $output Passed by reference. Used to append additional content.
 891       * @param object $page Not used.
 892       * @param int $depth Depth of category. Not used.
 893       * @param array $args Only uses 'list' for whether should append to output.
 894       */
 895  	function end_el( &$output, $page, $depth = 0, $args = array() ) {
 896          if ( 'list' != $args['style'] )
 897              return;
 898  
 899          $output .= "</li>\n";
 900      }
 901  
 902  }
 903  
 904  /**
 905   * Create HTML dropdown list of Categories.
 906   *
 907   * @package WordPress
 908   * @since 2.1.0
 909   * @uses Walker
 910   */
 911  class Walker_CategoryDropdown extends Walker {
 912      /**
 913       * @see Walker::$tree_type
 914       * @since 2.1.0
 915       * @var string
 916       */
 917      var $tree_type = 'category';
 918  
 919      /**
 920       * @see Walker::$db_fields
 921       * @since 2.1.0
 922       * @todo Decouple this
 923       * @var array
 924       */
 925      var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
 926  
 927      /**
 928       * @see Walker::start_el()
 929       * @since 2.1.0
 930       *
 931       * @param string $output Passed by reference. Used to append additional content.
 932       * @param object $category Category data object.
 933       * @param int $depth Depth of category. Used for padding.
 934       * @param array $args Uses 'selected' and 'show_count' keys, if they exist.
 935       */
 936  	function start_el( &$output, $category, $depth, $args, $id = 0 ) {
 937          $pad = str_repeat('&nbsp;', $depth * 3);
 938  
 939          $cat_name = apply_filters('list_cats', $category->name, $category);
 940          $output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";
 941          if ( $category->term_id == $args['selected'] )
 942              $output .= ' selected="selected"';
 943          $output .= '>';
 944          $output .= $pad.$cat_name;
 945          if ( $args['show_count'] )
 946              $output .= '&nbsp;&nbsp;('. $category->count .')';
 947          $output .= "</option>\n";
 948      }
 949  }
 950  
 951  //
 952  // Tags
 953  //
 954  
 955  /**
 956   * Retrieve the link to the tag.
 957   *
 958   * @since 2.3.0
 959   * @see get_term_link()
 960   *
 961   * @param int|object $tag Tag ID or object.
 962   * @return string Link on success, empty string if tag does not exist.
 963   */
 964  function get_tag_link( $tag ) {
 965      if ( ! is_object( $tag ) )
 966          $tag = (int) $tag;
 967  
 968      $tag = get_term_link( $tag, 'post_tag' );
 969  
 970      if ( is_wp_error( $tag ) )
 971          return '';
 972  
 973      return $tag;
 974  }
 975  
 976  /**
 977   * Retrieve the tags for a post.
 978   *
 979   * @since 2.3.0
 980   * @uses apply_filters() Calls 'get_the_tags' filter on the list of post tags.
 981   *
 982   * @param int $id Post ID.
 983   * @return array
 984   */
 985  function get_the_tags( $id = 0 ) {
 986      return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );
 987  }
 988  
 989  /**
 990   * Retrieve the tags for a post formatted as a string.
 991   *
 992   * @since 2.3.0
 993   * @uses apply_filters() Calls 'the_tags' filter on string list of tags.
 994   *
 995   * @param string $before Optional. Before tags.
 996   * @param string $sep Optional. Between tags.
 997   * @param string $after Optional. After tags.
 998   * @param int $id Optional. Post ID. Defaults to the current post.
 999   * @return string
1000   */
1001  function get_the_tag_list( $before = '', $sep = '', $after = '', $id = 0 ) {
1002      return apply_filters( 'the_tags', get_the_term_list( $id, 'post_tag', $before, $sep, $after ), $before, $sep, $after, $id );
1003  }
1004  
1005  /**
1006   * Retrieve the tags for a post.
1007   *
1008   * @since 2.3.0
1009   *
1010   * @param string $before Optional. Before list.
1011   * @param string $sep Optional. Separate items using this.
1012   * @param string $after Optional. After list.
1013   * @return string
1014   */
1015  function the_tags( $before = null, $sep = ', ', $after = '' ) {
1016      if ( null === $before )
1017          $before = __('Tags: ');
1018      echo get_the_tag_list($before, $sep, $after);
1019  }
1020  
1021  /**
1022   * Retrieve tag description.
1023   *
1024   * @since 2.8
1025   *
1026   * @param int $tag Optional. Tag ID. Will use global tag ID by default.
1027   * @return string Tag description, available.
1028   */
1029  function tag_description( $tag = 0 ) {
1030      return term_description( $tag );
1031  }
1032  
1033  /**
1034   * Retrieve term description.
1035   *
1036   * @since 2.8
1037   *
1038   * @param int $term Optional. Term ID. Will use global term ID by default.
1039   * @param string $taxonomy Optional taxonomy name. Defaults to 'post_tag'.
1040   * @return string Term description, available.
1041   */
1042  function term_description( $term = 0, $taxonomy = 'post_tag' ) {
1043      if ( !$term && ( is_tax() || is_tag() || is_category() ) ) {
1044          $term = get_queried_object();
1045          $taxonomy = $term->taxonomy;
1046          $term = $term->term_id;
1047      }
1048      $description = get_term_field( 'description', $term, $taxonomy );
1049      return is_wp_error( $description ) ? '' : $description;
1050  }
1051  
1052  /**
1053   * Retrieve the terms of the taxonomy that are attached to the post.
1054   *
1055   * @since 2.5.0
1056   *
1057   * @param int $id Post ID.
1058   * @param string $taxonomy Taxonomy name.
1059   * @return array|bool False on failure. Array of term objects on success.
1060   */
1061  function get_the_terms( $id, $taxonomy ) {
1062      global $post;
1063  
1064       $id = (int) $id;
1065  
1066      if ( !$id ) {
1067          if ( empty( $post->ID ) )
1068              return false;
1069          else
1070              $id = (int) $post->ID;
1071      }
1072  
1073      $terms = get_object_term_cache( $id, $taxonomy );
1074      if ( false === $terms ) {
1075          $terms = wp_get_object_terms( $id, $taxonomy );
1076          wp_cache_add($id, $terms, $taxonomy . '_relationships');
1077      }
1078  
1079      $terms = apply_filters( 'get_the_terms', $terms, $id, $taxonomy );
1080  
1081      if ( empty( $terms ) )
1082          return false;
1083  
1084      return $terms;
1085  }
1086  
1087  /**
1088   * Retrieve a post's terms as a list with specified format.
1089   *
1090   * @since 2.5.0
1091   *
1092   * @param int $id Post ID.
1093   * @param string $taxonomy Taxonomy name.
1094   * @param string $before Optional. Before list.
1095   * @param string $sep Optional. Separate items using this.
1096   * @param string $after Optional. After list.
1097   * @return string
1098   */
1099  function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
1100      $terms = get_the_terms( $id, $taxonomy );
1101  
1102      if ( is_wp_error( $terms ) )
1103          return $terms;
1104  
1105      if ( empty( $terms ) )
1106          return false;
1107  
1108      foreach ( $terms as $term ) {
1109          $link = get_term_link( $term, $taxonomy );
1110          if ( is_wp_error( $link ) )
1111              return $link;
1112          $term_links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
1113      }
1114  
1115      $term_links = apply_filters( "term_links-$taxonomy", $term_links );
1116  
1117      return $before . join( $sep, $term_links ) . $after;
1118  }
1119  
1120  /**
1121   * Display the terms in a list.
1122   *
1123   * @since 2.5.0
1124   *
1125   * @param int $id Post ID.
1126   * @param string $taxonomy Taxonomy name.
1127   * @param string $before Optional. Before list.
1128   * @param string $sep Optional. Separate items using this.
1129   * @param string $after Optional. After list.
1130   * @return null|bool False on WordPress error. Returns null when displaying.
1131   */
1132  function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
1133      $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
1134  
1135      if ( is_wp_error( $term_list ) )
1136          return false;
1137  
1138      echo apply_filters('the_terms', $term_list, $taxonomy, $before, $sep, $after);
1139  }
1140  
1141  /**
1142   * Check if the current post has any of given category.
1143   *
1144   * @since 3.1.0
1145   *
1146   * @param string|int|array $category Optional. The category name/term_id/slug or array of them to check for.
1147   * @param int|object $post Optional. Post to check instead of the current post.
1148   * @return bool True if the current post has any of the given categories (or any category, if no category specified).
1149   */
1150  function has_category( $category = '', $post = null ) {
1151      return has_term( $category, 'category', $post );
1152  }
1153  
1154  /**
1155   * Check if the current post has any of given tags.
1156   *
1157   * The given tags are checked against the post's tags' term_ids, names and slugs.
1158   * Tags given as integers will only be checked against the post's tags' term_ids.
1159   * If no tags are given, determines if post has any tags.
1160   *
1161   * 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)
1162   * Prior to v2.7, this function could only be used in the WordPress Loop.
1163   * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
1164   *
1165   * @since 2.6.0
1166   *
1167   * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
1168   * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
1169   * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1170   */
1171  function has_tag( $tag = '', $post = null ) {
1172      return has_term( $tag, 'post_tag', $post );
1173  }
1174  
1175  /**
1176   * Check if the current post has any of given terms.
1177   *
1178   * The given terms are checked against the post's terms' term_ids, names and slugs.
1179   * Terms given as integers will only be checked against the post's terms' term_ids.
1180   * If no terms are given, determines if post has any terms.
1181   *
1182   * @since 3.1.0
1183   *
1184   * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for.
1185   * @param string $taxonomy Taxonomy name
1186   * @param int|object $post Optional. Post to check instead of the current post.
1187   * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1188   */
1189  function has_term( $term = '', $taxonomy = '', $post = null ) {
1190      $post = get_post($post);
1191  
1192      if ( !$post )
1193          return false;
1194  
1195      $r = is_object_in_term( $post->ID, $taxonomy, $term );
1196      if ( is_wp_error( $r ) )
1197          return false;
1198  
1199      return $r;
1200  }


Generated: Fri May 25 03:56:23 2012 Hosted by follow the white rabbit.