wp_generate_tag_cloud()创建标签云

目录

描述

Returns an HTML string that makes a tag cloud.

wp_generate_tag_cloud() 描述

用法

<?php wp_generate_tag_cloud$tags$args ); ?>

Default Usage

 <?php $args = array(
    
'smallest'  => 8,
    
'largest'   => 22,
    
'unit'      => 'pt',
    
'number'    => 0,
    
'format'    => 'flat',
    
'separator' => '',
    
'orderby'   => 'name',
    
'order'     => 'ASC',
    
'topic_count_text_callback'  => 'default_topic_count_text',
    
'topic_count_scale_callback' => 'default_topic_count_scale',
    
'filter'    => ); ?>

wp_generate_tag_cloud() 用法

注意

源文件

wp_generate_tag_cloud() 函数的代码位于 wp-includes/category-template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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; }

wp_generate_tag_cloud() 源文件

相关