WordPress wp_generate_tag_cloud() Function

Beginner
4 min read
Updated: Nov 2026
3 examples
Template tag

Definition

In WordPress, wp_generate_tag_cloud() turns an array of terms into a tag cloud. Popular tags get a bigger font size.

It returns the cloud as a string, or as an array when format is array. It does not print it. Most sites use wp_tag_cloud(), which calls this function for you.

Syntax

Pass an array of term objects. Add an array of arguments to change the look.

WordPress
$cloud = wp_generate_tag_cloud( $tags );
$cloud = wp_generate_tag_cloud( $tags, array(
  'smallest' => 12,
  'largest'  => 24,
  'unit'     => 'px',
) );
CallWhat it does
wp_generate_tag_cloud( $tags )Returns the cloud with the default sizes.
wp_generate_tag_cloud( $tags, array( 'smallest' => 12, 'largest' => 24, 'unit' => 'px' ) )Sets your own font sizes.
wp_generate_tag_cloud( $tags, array( 'format' => 'array' ) )Returns an array of links.

Each term object needs a link property. get_tags() does not add it, so set it yourself, as shown in the examples.

Full declaration
WordPress
wp_generate_tag_cloud( WP_Term[] $tags, string|array $args = '' ): string|string[]

Parameters

ParameterDescription
$tagsRequired. An array of WP_Term objects. Each one needs count, name, and link.
smallestOptional. Font size of the least used tag. Default is 8.
largestOptional. Font size of the most used tag. Default is 22.
unitOptional. CSS unit for the size, such as pt or px. Default is pt.
formatOptional. flat, list, or array. Default is flat.
orderbyOptional. name or count. Default is name.
orderOptional. ASC, DESC, or RAND. Default is ASC.

Return Value

Return ValueDescription
stringThe cloud HTML. This is the result for flat and list formats.
string[]An array of links when format is array.

WordPress Examples

Three short theme examples. Each one has a sample output.

Example 1 — Basic cloud

Get tags, add links, and build the cloud.

WordPress
<?php
$tags = get_tags( array( 'number' => 2 ) );
foreach ( $tags as $tag ) {
  $tag->link = get_tag_link( $tag->term_id );
  $tag->id   = $tag->term_id;
}
echo wp_generate_tag_cloud( $tags, array(
  'smallest' => 12,
  'largest'  => 24,
  'unit'     => 'px',
) );
?>

How It Works

  1. get_tags() returns tag objects.
  2. The loop adds a link to each tag.
  3. The tag used most gets the largest size.

Example 2 — List format

Ask for an HTML list instead of plain links.

WordPress
<?php
echo wp_generate_tag_cloud( $tags, array(
  'format' => 'list',
  'unit'   => 'px',
) );
?>

How It Works

  1. 'format' => 'list' wraps the links in a ul.
  2. Each tag is one li.
  3. The $tags array is the same one prepared in Example 1.

Example 3 — Array format

Get an array of links and build your own markup.

WordPress
<?php
$links = wp_generate_tag_cloud( $tags, array( 'format' => 'array' ) );
foreach ( $links as $link ) {
  echo '<span class="chip">' . $link . '</span>';
}
?>

How It Works

  1. 'format' => 'array' returns an array of links.
  2. The loop wraps each link in a span.
  3. This gives you full control of the HTML.

Frequently Asked Questions

It builds tag cloud HTML from an array of term objects. Popular tags get a bigger font size.
wp_tag_cloud() fetches the tags for you and prints the cloud. wp_generate_tag_cloud() only builds the HTML from tags you give it.
Each term object needs a link property. Set it with get_tag_link() or get_term_link() before you call the function.
Yes. Use 'format' => 'array' to get an array of links.
Yes. Pass category term objects and set their link with get_category_link().

Next: wp_list_categories()

Print or return an HTML list of categories with links.

wp_list_categories() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful