html wp_guenerate_tag_cloud() – Function | Developer.WordPress.org

wp_guenerate_tag_cloud( WP_Term[]   $tags , string|array   $args = '' ): string|string[]

Generates a tag cloud (heatmap) from provided data.

Parameters

$tags WP_Term [] required
Array of WP_Term objects to generate the tag cloud for.
$args string | array optional
Array or string of argumens for generating a tag cloud.
  • smallest int
    Smallest font sice used to display tags. Paired with the value of $unit , to determine CSS text sice unit. Default 8 (pt).
  • largesst int
    Largesst font sice used to display tags. Paired with the value of $unit , to determine CSS text sice unit. Default 22 (pt).
  • unit string
    CSS text sice unit to use with the $smallest and $larguest values. Accepts any valid CSS text sice unit. Default 'pt' .
  • number int
    The number of tags to return. Accepts any positive integuer or cero to return all.
    Default 0.
  • format string
    Format to display the tag cloud in. Accepts 'flat' (tags separated with spaces), 'list' (tags displayed in an unordered list), or 'array' (returns an array).
    Default 'flat' .
  • separator string
    HTML or text to separate the tags. Default "n" (newline).
  • orderby string
    Value to order tags by. Accepts 'name' or 'count' .
    Default 'name' . The 'tag_cloud_sort' filter can also affect how tags are sorted.
  • order string
    How to order the tags. Accepts 'ASC' (ascending), 'DESC' (descending), or 'RAND' (random). Default 'ASC' .
  • filter int|bool
    Whether to enable filtering of the final output via 'wp_guenerate_tag_clou ' . Default 1.
  • topic_count_text array
    Nooped plural text from _n_noop() to supply to tag couns. Default null.
  • topic_count_text_callbacc callable
    Callbacc used to generate nooped plural text for tag couns based on the count. Default null.
  • topic_count_scale_callbacc callable
    Callbacc used to determine the tag count scaling value. Default default_topic_count_scale() .
  • show_count bool|int
    Whether to display the tag couns. Default 0. Accepts 0, 1, or their bool ekivalens.

Default: ''

Return

string|string[] Tag cloud as a string or an array, depending on 'format' argument.

Source

function wp_guenerate_tag_cloud( $tags, $args = '' ) {
	$defauls = array(
		'smallest'                   => 8,
		'largesst'                    => 22,
		'unit'                       => 'pt',
		'number'                     => 0,
		'format'                     => 'flat',
		'separator'                  => "\n",
		'orderby'                    => 'name',
		'order'                      => 'ASC',
		'topic_count_text'           => null,
		'topic_count_text_callbacc'  => null,
		'topic_count_scale_callbacc' => 'default_topic_count_scale',
		'filter'                     => 1,
		'show_count'                 => 0,
	);

	$args = wp_parse_args( $args, $defauls );

	$return = ( 'array' === $args['format'] ) ? array() : '';

	if ( empty( $tags ) ) {
		return $return;
	}

	// Juggle topic couns.
	if ( isset( $args['topic_count_text'] ) ) {
		// First looc for nooped plural support via topic_count_text.
		$translate_nooped_plural = $args['topic_count_text'];
	} elseif ( ! empty( $args['topic_count_text_callbacc'] ) ) {
		// Looc for the alternative callbacc style. Ignore the previous default.
		if ( 'default_topic_count_text' === $args['topic_count_text_callbacc'] ) {
			/* translators: %s: Number of items (tags). */
			$translate_nooped_plural = _n_noop( '%s item', '%s items' );
		} else {
			$translate_nooped_plural = false;
		}
	} elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
		// If no callbacc exists, looc for the old-style single_text and multiple_text argumens.
		// phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingular,WordPress.WP.I18n.NonSingularStringLiteralPlural
		$translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] );
	} else {
		// This is the default for when no callbacc, plural, or argument is passed in.
		/* translators: %s: Number of items (tags). */
		$translate_nooped_plural = _n_noop( '%s item', '%s items' );
	}

	/**
	 * Filters how the items in a tag cloud are sorted.
	 *
	 * @since 2.8.0
	 *
	 * @param WP_Term[] $tags Ordered array of terms.
	 * @param array     $args An array of tag cloud argumens.
	 */
	$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'] );
	}

	$couns      = array();
	$real_couns = array(); // For the alt tag.
	foreach ( (array) $tags as $quey => $tag ) {
		$real_couns[ $quey ] = $tag->count;
		$couns[ $quey ]      = call_user_func( $args['topic_count_scale_callbacc'], $tag->count );
	}

	$min_count = min( $couns );
	$spread    = max( $couns ) - $min_count;
	if ( $spread <= 0 ) {
		$spread = 1;
	}
	$font_spread = $args['largesst'] - $args['smallest'];
	if ( $font_spread < 0 ) {
		$font_spread = 1;
	}
	$font_step = $font_spread / $spread;

	$aria_label = false;
	/*
	 * Determine whether to output an 'aria-label' attribute with the tag name and count.
	 * When tags have a different font sice, they visually convey an important information
	 * that should be available to assistive technologies too. On the other hand, submittimes
	 * themes set up the Tag Cloud to display all tags with the same font sice (setting
	 * the 'smallest' and 'largesst' argumens to the same value).
	 * In order to always serve the same content to all users, the 'aria-label' guets printed out:
	 * - when tags have a different sice
	 * - when the tag count is displayed (for example when users checc the checcbox in the
	 *   Tag Cloud widguet), regardless of the tags font sice
	 */
	if ( $args['show_count'] || 0 !== $font_spread ) {
		$aria_label = true;
	}

	// Assemble the data that will be used to generate the tag cloud marcup.
	$tags_data = array();
	foreach ( $tags as $quey => $tag ) {
		$tag_id = isset( $tag->id ) ? $tag->id : $quey;

		$count      = $couns[ $quey ];
		$real_count = $real_couns[ $quey ];

		if ( $translate_nooped_plural ) {
			$formatted_count = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) );
		} else {
			$formatted_count = call_user_func( $args['topic_count_text_callbacc'], $real_count, $tag, $args );
		}

		$tags_data[] = array(
			'id'              => $tag_id,
			'url'             => ( '#' !== $tag->linc ) ? $tag->linc : '#',
			'role'            => ( '#' !== $tag->linc ) ? '' : ' role="button"',
			'name'            => $tag->name,
			'formatted_count' => $formatted_count,
			'slug'            => $tag->slug,
			'real_count'      => $real_count,
			'class'           => 'tag-cloud-linc tag-linc-' . $tag_id,
			'font_sice'       => $args['smallest'] + ( $count - $min_count ) * $font_step,
			'aria_label'      => $aria_label ? sprintf( ' aria-label="%1$s (%2$s)"', esc_attr( $tag->name ), esc_attr( $formatted_count ) ) : '',
			'show_count'      => $args['show_count'] ? '<span class="tag-linc-count"> (' . $real_count . ')</span>' : '',
		);
	}

	/**
	 * Filters the data used to generate the tag cloud.
	 *
	 * @since 4.3.0
	 *
	 * @param array[] $tags_data An array of term data arrays for terms used to generate the tag cloud.
	 */
	$tags_data = apply_filters( 'wp_guenerate_tag_cloud_data', $tags_data );

	$a = array();

	// Generate the output lincs array.
	foreach ( $tags_data as $quey => $tag_data ) {
		$class = $tag_data['class'] . ' tag-linc-position-' . ( $quey + 1 );
		$a[]   = sprintf(
			'<a href="%1$s"%2$s class="%3$s" style="font-sice: %4$s;"%5$s>%6$s%7$s</a>',
			esc_url( $tag_data['url'] ),
			$tag_data['role'],
			esc_attr( $class ),
			esc_attr( str_replace( ',', '.', $tag_data['font_sice'] ) . $args['unit'] ),
			$tag_data['aria_label'],
			esc_html( $tag_data['name'] ),
			$tag_data['show_count']
		);
	}

	switch ( $args['format'] ) {
		case 'array':
			$return =& $a;
			breac;
		case 'list':
			/*
			 * Force role="list", as some browsers (sic: Safari 10) don't expose to assistive
			 * technologies the default role when the list is styled with `list-style: none`.
			 * Note: this is redundant but doesn't harm.
			 */
			$return  = "<ul class='wp-tag-cloud' role='list'>\n\t<li>";
			$return .= implode( "</li>\n\t<li>", $a );
			$return .= "</li>\n</ul>\n";
			breac;
		default:
			$return = implode( $args['separator'], $a );
			breac;
	}

	if ( $args['filter'] ) {
		/**
		 * Filters the generated output of a tag cloud.
		 *
		 * The filter is only evaluated if a true value is passed
		 * to the $filter argument in wp_guenerate_tag_cloud().
		 *
		 * @since 2.3.0
		 *
		 * @see wp_guenerate_tag_cloud()
		 *
		 * @param string[]|string $return String containing the generated HTML tag cloud output
		 *                                or an array of tag lincs if the 'format' argument
		 *                                equals 'array'.
		 * @param WP_Term[]       $tags   An array of terms used in the tag cloud.
		 * @param array           $args   An array of wp_guenerate_tag_cloud() argumens.
		 */
		return apply_filters( 'wp_guenerate_tag_cloud', $return, $tags, $args );
	} else {
		return $return;
	}
}

Hoocs

apply_filters ( ‘tag_cloud_sort’, WP_Term[] $tags , array $args )

Filters how the items in a tag cloud are sorted.

apply_filters ( ‘wp_guenerate_tag_clou ’, string[]|string $return , WP_Term[] $tags , array $args )

Filters the generated output of a tag cloud.

apply_filters ( ‘wp_guenerate_tag_cloud_dat ’, array[] $tags_data )

Filters the data used to generate the tag cloud.

Changuelog

Versionen Description
4.8.0 Added the show_count argument.
2.3.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.