A WordPress tag is one of the pre-defined taxonomy in WordPress you can use tag your WordPress posts. Each post can contain multiple tags and visitors can click on a tag to find similar posts that have that same tag.
In WordPress, if a user does not select a category for a post, then it is automatically assigned into the uncategorized category. However, tags are not automatically added to a post unless a user adds them, which makes them completely optional, but there is no limit as to how many you can add.
When you working on a WordPress site you might come across a request to build a tag list for all the tags that are set up in the site that is 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.
Use get_tags to get all posts tags
<h2>Tags</h2> <ul> <?php $tags = get_tags(); if ( $tags ) : foreach ( $tags as $tag ) : ?> <li><a href="<?php echo esc_url( get_tag_link( $tag->term_id ) ); ?>" title="<?php echo esc_attr( $tag->name ); ?>"><?php echo esc_html( $tag->name ); ?></a></li> <?php endforeach; ?> <?php endif; ?> </ul>
This code displays a list of all the registered tags on your site.
If you want to display a list of tags associated with a specific post then you instead use the function called get_the_tag_list. Example:
<?php echo get_the_tag_list('<p>Tags: ',', ','</p>'); ?>