When working on a WordPress site you might come across a request to build a tag list/cloud/directory for all the tags that are setup in the site that are associated with at least one or more posts.
We looked everywhere for code that would do this and we kept running across the same examples that only showed how to display the tags associated with a single post not all posts.
But then we found it and it was so simple, we created a new WordPress page using a custom template. In that template we placed the following php code.
<ul id="Genre-List"> <?php $tags = get_tags(); foreach ( $tags as $tag ) : $tag_link = get_tag_link( $tag->term_id ); ?> <li> <a href='<?php echo $tag_link; ?>' title='<?php echo $tag->name; ?>' class='<?php echo $tag->slug ?>'><?php echo $tag->name ?></a> </li> <?php endforeach; ?> </ul>
The tags are already sorted alphabetically when calling the get_tags(); function.
Get Tags Function
With this function and the use of a simple foreach loop you can retrieve information each tag that is associated with your posts in WordPress.
You can pass a variety of different parameters inside the open and closing () of the function call.
- Default is 'name'. Can be name, count, or nothing (will use term_id).
- Default is ASC. Can use DESC.
- Default is true. Will not return empty terms, which means terms whose count is 0 according to the given taxonomy.
- Default is an empty string. A comma- or space-delimited string of term ids to exclude from the return array. If 'include' is non-empty, 'exclude' is ignored.
- Default is an empty string. A comma- or space-delimited string of term ids to include in the return array.
- The maximum number of terms to return. Default is empty.
- The number by which to offset the terms query.
- Default is 'all', which returns an array of term objects. If 'fields' is 'ids' or 'names', returns an array of integers or strings, respectively.
- Returns terms whose "slug" matches this value. Default is empty string.
- Whether to include terms that have non-empty descendants (even if 'hide_empty' is set to true).
- Returned terms' names will contain the value of 'search', case-insensitive. Default is an empty string.
- Returned terms' names will contain the value of 'name__like', case-sensitive. Default is empty string.
- Returned terms' descriptions will contain the value of 'description__like', case-insensitive. Default is empty string.
- If set to true will include the quantity of a term's children in the quantity of each term's "count" object variable.
- If set to 'all' instead of its default empty string, returns terms regardless of ancestry or whether the terms are empty.
- When used, should be set to the integer of a term ID. Its default is 0. If set to a non-zero value, all returned terms will be descendants of that term according to the given taxonomy. Hence 'child_of' is set to 0 if more than one taxonomy is passed in $taxonomies, because multiple taxonomies make term ancestry ambiguous.
- When used, should be set to the integer of a term ID. Its default is the empty string, which has a different meaning from the integer 0. If set to an integer value, all returned terms will have as an immediate ancestor the term whose ID is specified by that integer according to the given taxonomy. The 'parent' argument is different from 'child_of' in that a term X is considered a 'parent' of term Y only if term X is the father of term Y, not its grandfather or great-grandfather, etc.
Inside of the foreach loop you can reference any of the following:
$tag->term_id $tag->name $tag->slug $tag->term_group $tag->term_taxonomy_id $tag->taxonomy $tag->description $tag->parent $tag->count