Home WordPress Category Functions wp_generate_tag_cloud() WordPress wp_generate_tag_cloud() Function 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.
$cloud = wp_generate_tag_cloud( $tags );
$cloud = wp_generate_tag_cloud( $tags, array(
'smallest' => 12,
'largest' => 24,
'unit' => 'px',
) ); Call What 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 wp_generate_tag_cloud( WP_Term[] $tags, string|array $args = '' ): string|string[] Parameters Parameter Description $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 Value Description string The 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.
1. Basic cloud 2. List format 3. Array format Example 1 — Basic cloud Get tags, add links, and build the cloud.
<?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',
) );
?> <a href="https://example.com/tag/php/" class="tag-cloud-link tag-link-7 tag-link-position-1" style="font-size: 12px;" aria-label="PHP (2 items)">PHP</a>
<a href="https://example.com/tag/wordpress/" class="tag-cloud-link tag-link-8 tag-link-position-2" style="font-size: 24px;" aria-label="WordPress (6 items)">WordPress</a> How It Works get_tags() returns tag objects.The loop adds a link to each tag. The tag used most gets the largest size. Example 2 — List format Ask for an HTML list instead of plain links.
<?php
echo wp_generate_tag_cloud( $tags, array(
'format' => 'list',
'unit' => 'px',
) );
?> <ul class='wp-tag-cloud' role='list'>
<li><a href="https://example.com/tag/php/" class="tag-cloud-link tag-link-7 tag-link-position-1" style="font-size: 8px;" aria-label="PHP (2 items)">PHP</a></li>
<li><a href="https://example.com/tag/wordpress/" class="tag-cloud-link tag-link-8 tag-link-position-2" style="font-size: 22px;" aria-label="WordPress (6 items)">WordPress</a></li>
</ul> How It Works 'format' => 'list' wraps the links in a ul.Each tag is one li. 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.
<?php
$links = wp_generate_tag_cloud( $tags, array( 'format' => 'array' ) );
foreach ( $links as $link ) {
echo '<span class="chip">' . $link . '</span>';
}
?> <span class="chip"><a href="https://example.com/tag/php/" class="tag-cloud-link tag-link-7 tag-link-position-1" style="font-size: 8pt;" aria-label="PHP (2 items)">PHP</a></span><span class="chip"><a href="https://example.com/tag/wordpress/" class="tag-cloud-link tag-link-8 tag-link-position-2" style="font-size: 22pt;" aria-label="WordPress (6 items)">WordPress</a></span> How It Works 'format' => 'array' returns an array of links.The loop wraps each link in a span. This gives you full control of the HTML. Frequently Asked Questions What does wp_generate_tag_cloud() do? It builds tag cloud HTML from an array of term objects. Popular tags get a bigger font size.
What is the difference between wp_generate_tag_cloud() and wp_tag_cloud()? 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.
Why do my links show # ? Each term object needs a link property. Set it with get_tag_link() or get_term_link() before you call the function.
Can I get an array instead of a string? Yes. Use 'format' => 'array' to get an array of links.
Can I use it for categories? Yes. Pass category term objects and set their link with get_category_link().
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion