wp_generate_tag_cloud()
wp_generate_tag_cloud( WP_Term[] $tags, string|array $a…
wp_generate_tag_cloud( WP_Term[] $tags, string|array $args = ” )
从提供的数据生成标记云(热图)。
Generates a tag cloud (heatmap) from provided data.
目录锚点:#源码
源码(Source)
/** * Generates a tag cloud (heatmap) from provided data. * * The text size is set by the 'smallest' and 'largest' arguments, which will * use the 'unit' argument value for the CSS text size unit. The 'format' * argument can be 'flat' (default), 'list', or 'array'. The flat value for the * 'format' argument will separate tags with spaces. The list value for the * 'format' argument will format the tags in a UL HTML list. The array value for * the 'format' argument will return in PHP array type format. * * The 'tag_cloud_sort' filter allows you to override the sorting. * Passed to the filter: $tags array and $args array, has to return the $tags array * after sorting it. * * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'. * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or * 'RAND'. * * The 'number' argument is how many tags to return. By default, the limit will * be to return the entire tag cloud list. * * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the * text for the tooltip of the tag link. * * The 'topic_count_text_callback' argument is a function, which given the count * of the posts with that tag returns a text for the tooltip of the tag link. * * @todo Complete functionality. * @since 2.3.0 * * @param array $tags List of tags. * @param string|array $args Optional, override default arguments. * @return string|array Tag cloud as a string or an array, depending on 'format' argument. */ function wp_generate_tag_cloud( $tags, $args = '' ) { $defaults = array( 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0, 'format' => 'flat', 'separator' => " ", 'orderby' => 'name', 'order' => 'ASC', 'topic_count_text' => null, 'topic_count_text_callback' => null, 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1, ); $args = wp_parse_args( $args, $defaults ); $return = ( 'array' === $args['format'] ) ? array() : ''; if ( empty( $tags ) ) { return $return; } // Juggle topic count tooltips: if ( isset( $args['topic_count_text'] ) ) { // First look for nooped plural support via topic_count_text. $translate_nooped_plural = $args['topic_count_text']; } elseif ( ! empty( $args['topic_count_text_callback'] ) ) { // Look for the alternative callback style. Ignore the previous default. if ( $args['topic_count_text_callback'] === 'default_topic_count_text' ) { $translate_nooped_plural = _n_noop( '%s topic', '%s topics' ); } else { $translate_nooped_plural = false; } } elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) { // If no callback exists, look for the old-style single_text and multiple_text arguments. $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] ); } else { // This is the default for when no callback, plural, or argument is passed in. $translate_nooped_plural = _n_noop( '%s topic', '%s topics' ); } /** * Filter how the items in a tag cloud are sorted. * * @since 2.8.0 * * @param array $tags Ordered array of terms. * @param array $args An array of tag cloud arguments. */ $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args ); if ( empty( $tags_sorted ) ) { return $return; } if ( $tags_sorted !== $tags ) { $tags = $tags_sorted; unset( $tags_sorted ); } else { if ( 'RAND' === $args['order'] ) { shuffle( $tags ); } else { // SQL cannot save you; this is a second (potentially different) sort on a subset of data. if ( 'name' === $args['orderby'] ) { uasort( $tags, '_wp_object_name_sort_cb' ); } else { uasort( $tags, '_wp_object_count_sort_cb' ); } if ( 'DESC' === $args['order'] ) { $tags = array_reverse( $tags, true ); } } } if ( $args['number'] > 0 ) $tags = array_slice( $tags, 0, $args['number'] ); $counts = array(); $real_counts = array(); // For the alt tag foreach ( (array) $tags as $key => $tag ) { $real_counts[ $key ] = $tag->count; $counts[ $key ] = call_user_func( $args['topic_count_scale_callback'], $tag->count ); } $min_count = min( $counts ); $spread = max( $counts ) - $min_count; if ( $spread <= 0="" )="" $spread="1;" $font_spread="$args['largest']" -="" $args['smallest'];="" if="" (="" $font_spread="">< 0="" )="" $font_spread="1;" $font_step="$font_spread" $spread;="" assemble="" the="" data="" that="" will="" be="" used="" to="" generate="" the="" tag="" cloud="" markup.="" $tags_data="array();" foreach="" (="" $tags="" as="" $key=""> $tag ) { $tag_id = isset( $tag->id ) ? $tag->id : $key; $count = $counts[ $key ]; $real_count = $real_counts[ $key ]; if ( $translate_nooped_plural ) { $title = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) ); } else { $title = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args ); } $tags_data[] = array( 'id' => $tag_id, 'url' => '#' != $tag->link ? $tag->link : '#', 'name' => $tag->name, 'title' => $title, 'slug' => $tag->slug, 'real_count' => $real_count, 'class' => 'tag-link-' . $tag_id, 'font_size' => $args['smallest'] + ( $count - $min_count ) * $font_step, ); } /** * Filter the data used to generate the tag cloud. * * @since 4.3.0 * * @param array $tags_data An array of term data for term used to generate the tag cloud. */ $tags_data = apply_filters( 'wp_generate_tag_cloud_data', $tags_data ); $a = array(); // generate the output links array foreach ( $tags_data as $key => $tag_data ) { $a[] = "" . esc_html( $tag_data['name'] ) . ""; } switch ( $args['format'] ) { case 'array' : $return =& $a; break; case 'list' : $return = " "; $return .= join( " ", $a ); $return .= " "; break; default : $return = join( $args['separator'], $a ); break; } if ( $args['filter'] ) { /** * Filter the generated output of a tag cloud. * * The filter is only evaluated if a true value is passed * to the $filter argument in wp_generate_tag_cloud(). * * @since 2.3.0 * * @see wp_generate_tag_cloud() * * @param array|string $return String containing the generated HTML tag cloud output * or an array of tag links if the 'format' argument * equals 'array'. * @param array $tags An array of terms used in the tag cloud. * @param array $args An array of wp_generate_tag_cloud() arguments. */ return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args ); } else return $return; }
更新版本 | 源码位置 | 使用 | 被使用 |
---|---|---|---|
4.8.0 | wp-includes/category-template.php | 2 | 20 |
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!