521 * @return array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument. |
526 * @return array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument. |
522 */ |
527 */ |
523 function wp_tag_cloud( $args = '' ) { |
528 function wp_tag_cloud( $args = '' ) { |
524 $defaults = array( |
529 $defaults = array( |
525 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, |
530 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, |
526 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC', |
531 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC', |
527 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true |
532 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true |
528 ); |
533 ); |
529 $args = wp_parse_args( $args, $defaults ); |
534 $args = wp_parse_args( $args, $defaults ); |
530 |
535 |
531 $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags |
536 $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags |
564 function default_topic_count_text( $count ) { |
569 function default_topic_count_text( $count ) { |
565 return sprintf( _n('%s topic', '%s topics', $count), number_format_i18n( $count ) ); |
570 return sprintf( _n('%s topic', '%s topics', $count), number_format_i18n( $count ) ); |
566 } |
571 } |
567 |
572 |
568 /** |
573 /** |
|
574 * Default topic count scaling for tag links |
|
575 * |
|
576 * @param integer $count number of posts with that tag |
|
577 * @return integer scaled count |
|
578 */ |
|
579 function default_topic_count_scale( $count ) { |
|
580 return round(log10($count + 1) * 100); |
|
581 } |
|
582 |
|
583 |
|
584 /** |
569 * Generates a tag cloud (heatmap) from provided data. |
585 * Generates a tag cloud (heatmap) from provided data. |
570 * |
586 * |
571 * The text size is set by the 'smallest' and 'largest' arguments, which will |
587 * The text size is set by the 'smallest' and 'largest' arguments, which will |
572 * use the 'unit' argument value for the CSS text size unit. The 'format' |
588 * use the 'unit' argument value for the CSS text size unit. The 'format' |
573 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the |
589 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the |
574 * 'format' argument will separate tags with spaces. The list value for the |
590 * 'format' argument will separate tags with spaces. The list value for the |
575 * 'format' argument will format the tags in a UL HTML list. The array value for |
591 * 'format' argument will format the tags in a UL HTML list. The array value for |
576 * the 'format' argument will return in PHP array type format. |
592 * the 'format' argument will return in PHP array type format. |
577 * |
593 * |
578 * The 'tag_cloud_sort' filter allows you to override the sorting done |
594 * The 'tag_cloud_sort' filter allows you to override the sorting. |
579 * by the 'orderby' argument; passed to the filter: $tags array and $args array. |
595 * Passed to the filter: $tags array and $args array, has to return the $tags array |
|
596 * after sorting it. |
580 * |
597 * |
581 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'. |
598 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'. |
582 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or |
599 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or |
583 * 'RAND'. |
600 * 'RAND'. |
584 * |
601 * |
597 */ |
614 */ |
598 function wp_generate_tag_cloud( $tags, $args = '' ) { |
615 function wp_generate_tag_cloud( $tags, $args = '' ) { |
599 global $wp_rewrite; |
616 global $wp_rewrite; |
600 $defaults = array( |
617 $defaults = array( |
601 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0, |
618 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0, |
602 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC', |
619 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC', |
603 'topic_count_text_callback' => 'default_topic_count_text', |
620 'topic_count_text_callback' => 'default_topic_count_text', |
604 'filter' => 1, |
621 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1, |
605 ); |
622 ); |
606 |
623 |
607 if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) { |
624 if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) { |
608 $body = 'return sprintf ( |
625 $body = 'return sprintf ( |
609 _n('.var_export($args['single_text'], true).', '.var_export($args['multiple_text'], true).', $count), |
626 _n(' . var_export($args['single_text'], true) . ', ' . var_export($args['multiple_text'], true) . ', $count), |
610 number_format_i18n( $count ));'; |
627 number_format_i18n( $count ));'; |
611 $args['topic_count_text_callback'] = create_function('$count', $body); |
628 $args['topic_count_text_callback'] = create_function('$count', $body); |
612 } |
629 } |
613 |
630 |
614 $args = wp_parse_args( $args, $defaults ); |
631 $args = wp_parse_args( $args, $defaults ); |
615 |
|
616 extract( $args ); |
632 extract( $args ); |
617 |
633 |
618 if ( empty( $tags ) ) |
634 if ( empty( $tags ) ) |
619 return; |
635 return; |
620 |
636 |
621 // SQL cannot save you; this is a second (potentially different) sort on a subset of data. |
637 $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args ); |
622 if ( 'name' == $orderby ) |
638 if ( $tags_sorted != $tags ) { // the tags have been sorted by a plugin |
623 uasort( $tags, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);') ); |
639 $tags = $tags_sorted; |
624 else |
640 unset($tags_sorted); |
625 uasort( $tags, create_function('$a, $b', 'return ($a->count > $b->count);') ); |
641 } else { |
626 |
642 if ( 'RAND' == $order ) { |
627 $tags = apply_filters( 'tag_cloud_sort', $tags, $args ); |
643 shuffle($tags); |
628 |
644 } else { |
629 if ( 'DESC' == $order ) |
645 // SQL cannot save you; this is a second (potentially different) sort on a subset of data. |
630 $tags = array_reverse( $tags, true ); |
646 if ( 'name' == $orderby ) |
631 elseif ( 'RAND' == $order ) { |
647 uasort( $tags, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);') ); |
632 $keys = (array) array_rand( $tags, count( $tags ) ); |
648 else |
633 $temp = array(); |
649 uasort( $tags, create_function('$a, $b', 'return ($a->count > $b->count);') ); |
634 foreach ( $keys as $key ) |
650 |
635 $temp[$key] = $tags[$key]; |
651 if ( 'DESC' == $order ) |
636 |
652 $tags = array_reverse( $tags, true ); |
637 $tags = $temp; |
653 } |
638 $temp = null; |
|
639 unset( $temp ); |
|
640 } |
654 } |
641 |
655 |
642 if ( $number > 0 ) |
656 if ( $number > 0 ) |
643 $tags = array_slice($tags, 0, $number); |
657 $tags = array_slice($tags, 0, $number); |
644 |
658 |
645 $counts = array(); |
659 $counts = array(); |
646 foreach ( (array) $tags as $key => $tag ) |
660 $real_counts = array(); // For the alt tag |
647 $counts[ $key ] = $tag->count; |
661 foreach ( (array) $tags as $key => $tag ) { |
|
662 $real_counts[ $key ] = $tag->count; |
|
663 $counts[ $key ] = $topic_count_scale_callback($tag->count); |
|
664 } |
648 |
665 |
649 $min_count = min( $counts ); |
666 $min_count = min( $counts ); |
650 $spread = max( $counts ) - $min_count; |
667 $spread = max( $counts ) - $min_count; |
651 if ( $spread <= 0 ) |
668 if ( $spread <= 0 ) |
652 $spread = 1; |
669 $spread = 1; |
655 $font_spread = 1; |
672 $font_spread = 1; |
656 $font_step = $font_spread / $spread; |
673 $font_step = $font_spread / $spread; |
657 |
674 |
658 $a = array(); |
675 $a = array(); |
659 |
676 |
660 $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : ''; |
|
661 |
|
662 foreach ( $tags as $key => $tag ) { |
677 foreach ( $tags as $key => $tag ) { |
663 $count = $counts[ $key ]; |
678 $count = $counts[ $key ]; |
|
679 $real_count = $real_counts[ $key ]; |
664 $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#'; |
680 $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#'; |
665 $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key; |
681 $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key; |
666 $tag_name = $tags[ $key ]->name; |
682 $tag_name = $tags[ $key ]->name; |
667 $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( $topic_count_text_callback( $count ) ) . "'$rel style='font-size: " . |
683 $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( $topic_count_text_callback( $real_count ) ) . "' style='font-size: " . |
668 ( $smallest + ( ( $count - $min_count ) * $font_step ) ) |
684 ( $smallest + ( ( $count - $min_count ) * $font_step ) ) |
669 . "$unit;'>$tag_name</a>"; |
685 . "$unit;'>$tag_name</a>"; |
670 } |
686 } |
671 |
687 |
672 switch ( $format ) : |
688 switch ( $format ) : |
910 * @param string $before Optional. Before list. |
927 * @param string $before Optional. Before list. |
911 * @param string $sep Optional. Separate items using this. |
928 * @param string $sep Optional. Separate items using this. |
912 * @param string $after Optional. After list. |
929 * @param string $after Optional. After list. |
913 * @return null|bool False on WordPress error. Returns null when displaying. |
930 * @return null|bool False on WordPress error. Returns null when displaying. |
914 */ |
931 */ |
915 function the_terms( $id, $taxonomy, $before = '', $sep = '', $after = '' ) { |
932 function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) { |
916 $return = get_the_term_list( $id, $taxonomy, $before, $sep, $after ); |
933 $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after ); |
917 if ( is_wp_error( $return ) ) |
934 |
|
935 if ( is_wp_error( $term_list ) ) |
918 return false; |
936 return false; |
919 else |
937 |
920 echo $return; |
938 echo apply_filters('the_terms', $term_list, $taxonomy, $before, $sep, $after); |
921 } |
939 } |
922 |
940 |
923 /** |
941 /** |
924 * Check if the current post has any of given tags. |
942 * Check if the current post has any of given tags. |
925 * |
943 * |