wordpress tags

WordPress Tags PHP Development

A WordPress tag is one of the default tools you can use to categorize your WordPress posts based on similar details. Tags in WordPress are something that you will always find in the tag list in the right sidebar when writing or editing a post like categories. WordPress Tags are one of the default taxonomies that can be used to organize your blog post, SEO optimization, and other Search Engines. They are normally made up of one or two words and describe specific details of a post.

Categories and tags are the two primary ways to group content on a WordPress site. In simple terms, categories are general labels, while tags are more specific (describe your posts in more detail).

For example, if you wrote a post about a book, then you could use tags to describe the author, publisher, and topics covered.

What are WordPress tags?

Tags are a great mechanism if you want to identify a piece of content by some specific keywords. Simply, pick a few words that describe a given post the best.

Usually, tags are located under a post or in the sidebar. When a visitor clicks a particular tag, WordPress will open an archive page (tag page) – indexing all the posts and custom post types that have the same tags.

Tags play a vital role in organizing your content. They have a direct impact on your user experience and SEO.

Tags provide a useful way to group related posts together and quickly tell readers what a post is about. Tags also make it easier for people to find your content.

Each post can contain multiple tags and visitors can click on a tag to find similar posts that have that same tag.

The main difference between categories and tags is the way you use them. Unlike WordPress categories, tags are completely optional. That is, you’re free to add WordPress tags to your post, but you can also publish a post without tags.

Two more important things to note about WordPress tags:

  • By default, tags are a feature of your posts in WordPress.
  • Tags don’t apply to your pages (but we’ll cover more on that in a bit).

How to add a tag or tags to a post in WordPress?

Adding tags to your WordPress posts is pretty straightforward. While creating or editing a post, you can simply type your desired tags into the WordPress editor in the “Tags” field. To add tags to a new post, go to your blog’s admin area > Posts > Add New.

When you write your new post, add a tag to it by typing the tag word in the Tags field on the right and clicking on Add. You can add as many tags as you want.

You can create and manage tags from the Posts » Tags page on your Admin sidebar too. You will notice the Add New Tag section on the left.

Here you can type the name and slug of your new tag. A slug is the permalink for the tag and helps identify it in your tag archives by giving that specific tag URL.

You can use the Description box to explain further what your tag is about. The description is optional and can be as short or as long as you want.

Do WordPress tags help SEO

WordPress tags and categories work together to achieve better ranking on search engine results pages (SERPs), boost your page views, and offer a better user experience and SEO.

In many cases, tags are either overused or completely ignored by WordPress users. Users who abuse and overuse tags in WordPress are normally under the impression that tags are some sort of a magic bullet for SEO (search engine optimization) success, like a meta tag.

Then there are the users that completely ignore post tags. They don’t see any practical use for them whatsoever. Because of this, they pay no attention to the feature and miss out on a couple of key benefits of properly using post tags.

  • For those who over-tag for SEO purposes: Unfortunately, this article is probably going to disappoint you if you spend a lot of time strategically tagging all of your posts for search engine optimization purposes. The fact of the matter is that WordPress tags and SEO are not inextricably linked.
  • For those who ignore tags: You’re missing out on some of the benefits of WordPress tags. While using tags certainly isn’t going to change your life, the fact is that they are an added piece of the puzzle for those developers who are serious about creating a popular, easy-to-navigate WordPress website.

When it comes to WordPress tags and SEO, the news isn’t what you might expect: Google doesn’t seem to give any special consideration to post tags, and they don’t do anything directly to raise your site’s SERP (Search Engine Results Page) rankings.

How to add a tag to the menu in WordPress

Adding a tag to your WordPress menu bar allows readers to easily find your content and navigate to a related group of blog posts like categories.

Go back to your Dashboard > Appearance > Menus. Expand the tags panel on the left-hand side and you’ll see all of the tags.

Select the tags that you’d like to display on the menu and click the ADD TO MENU button.

If there is no tags panel, expand the screen options on the top and check the “Tags”. Now you can find the tags panel to add the tag in the menu in WordPress.

Display tags in the post

Displaying tags can be done in some ways. One of them is in the post. When you are going to read a WordPress post, you could see that this post belongs to some tags (one or more). To display these tags in your post loop in WordPress you could use the below code.

while ( have_posts() ) :
        $tags = get_the_tags();
	foreach($tags as $tag){
		echo $tag->name . wp_get_list_item_separator();


Tag 1,Tag 2,Tag 3,

The get_the_tags() built-in WordPress function returns WP_Term which is the list of details like term_id, name, slug, description, and so on of the tag.

  • term_id
  • name
  • slug
  • term_group
  • term_taxonomy_id
  • taxonomy
  • category
  • description
  • parent
  • count
  • filter
  • cat_ID
  • category_count
  • category_description
  • cat_name
  • category_nicename
  • category_parent

If you wanna use this function outside the loop, you have to pass the post id as a parameter.

get_the_tags($post_id );

In this example, you have to control the separator yourself. The other thing is that the tag’s name is not linked and clickable it is better to use the get_the_tag_list() function instead.

There is another neat and convenient way to display post categories inside the loop in WordPress. The get_the_category_list() function displays the categories with extra options.

while ( have_posts() ) :
        $tags_list = get_the_tag_list(
			'Tags: ', 
			_x(wp_get_list_item_separator(), 'Used between list items.', 
        if ( $tags_list ) {


Tags: Tag 1,Tag 2,Tag 3

In this example, you could display WordPress tag list names, and the names are linked to the tag list display section. Also, you can use this method to display WordPress custom post type tags.

How to create a tag template archive page in WordPress

With WordPress websites, it’s common to use a different template archive page for a category, tag, custom post type, and taxonomy. By creating templates for tags, you can add specific features to tag archive pages.

For example, you can allow users to subscribe to tags, add tag images, show tag descriptions, and choose a different layout for each tag.

To create and design a WordPress tag archive template, you need to create a page template for that particular tag. Go to your theme’s folder. From the list of theme files, open the tag.php file, if you do not have a tag.php file there, then create one.

In the case of tags, the hierarchy is fairly simple. For instance, suppose the slug of the tag is the news and the Tag ID is 6. The Template Hierarchy specifies that WordPress will use the first template file it finds in your current theme’s directory from the following list:

  1. tag-slug.php
  2. tag-ID.php
  3. tag.php
  4. archive.php
  5. index.php

That is, if you do not have a tag-slug.php (let’s say tag-news.php), WordPress will check for a tag-ID.php (like tag-6.php), and so on.

Now open the tag-news.php file and add the following codes to it.

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
<div class="wrapper">
  <div class="primary-content">
    <h1 class="archive-title"><?php echo apply_filters( 'the_title', $term->name ); ?> News</h1>

    <?php if ( !empty( $term->description ) ): ?>
    <div class="archive-description">
      <?php echo esc_html($term->description); ?>
    <?php endif; ?>

    <?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>

    <div id="post-<?php the_ID(); ?>" <?php post_class('post'); ?>>
      <h2 class="post-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
        <div class="post-info">
          <p><?php the_time(get_option('date_format')); ?> by <?php the_author_posts_link(); ?></p>
        <div class="entry">
          <?php the_content( __('Full story…') ); ?>

    <?php endwhile; ?>

    <?php else: ?>

    <h2 class="post-title">No Post Type in <?php echo apply_filters( 'the_title', $term->name ); ?></h2>
    <div class="content clearfix">
      <div class="entry">
        <p>It seems there isn't anything happening in <strong><?php echo apply_filters( 'the_title', $term->name ); ?></strong> right now. Check back later, something is bound to happen soon.</p>

    <?php endif; ?>

<?php get_footer(); ?>

This is an example of the WordPress tag template for displaying the list and archive of tag posts.

How to display all tags in WordPress?

If you want to display all tag details list of WordPress, you could use the get_tags() function. For example, you have a widget that displays all tags list of WordPress. This function retrieves all post tags from WordPress.

$tags = get_tags();
foreach($tags as $tag){
	echo $tag->name . wp_get_list_item_separator();

How to display all tags list of the custom post type in WordPress?

Custom post types are like posts and pages. Let’s assume that you have created a custom post type and created a custom taxonomy as a tag(named “hstag”). With this code, you can retrieve all tags of the custom post type.

$args = array(
	'taxonomy' => 'hstag',
	'hide_empty' => false,
	'orderby' => 'name',
	'order'   => 'ASC'
$tags = get_tags($args);
foreach($tags as $tag){
	echo esc_html($tag->name) . wp_get_list_item_separator();

The “hstag” is the custom taxonomy slug of your custom post type.

How to create a WordPress tag programmatically

If you want to create a tag in WordPress by code, simply you can use the wp_insert_term() function like below.

	// the name of the tag
	'Tag A', 		
	// the taxonomy, which in this case if tag (don't change)
		'slug' => 'tag-a',  

If you want to create a tag for your custom post type in WordPress, just change the “post_tag” to your post type tag slug. Which in this case is “hstag”.

	// the name of the category
	'Tag A', 		
	// the taxonomy, which in this case if category (don't change)
		'slug' => 'tag-a',  

How to delete a tag programmatically in WordPress

To delete a tag by code, use the below code.

$tag_ID = 163;
if(wp_delete_term( $tag_ID, 'post_tag' )){
	echo "Tag #$tag_ID was successfully deleted";
} else {
	echo "Tag #$tag_ID deletion failed";

The wp_delete_term() WordPress built-in function is used to delete a tag.

To delete a tag from your custom post type simply change “post_tag” to your tag slug which is the “hstag” in our example.

$tag_ID = 163;
if(wp_delete_term( $tag_ID, 'hstag' )){
	echo "Tag #$tag_ID was successfully deleted";
} else {
	echo "Tag #$tag_ID deletion failed";

How to get tag link by ID

In this example, you have your tag ID and you want to get the tag link by its ID.

get_tag_link( $tag_id );

How to get tag slug by ID in WordPress

You can get the WordPress category slug and other details by ID with the get_category() function.

$tag = get_tag($tag_id);
echo $tag->slug;

This function returns WP_Term which is the list of details like term_id, name, slug, and so on that, we discussed above.

Check if the page is a tag page by the is_tag() function

// When any Tag archive page is being displayed.

// When the archive page for Tag 30 is being displayed.
is_tag( '30' );

// When the archive page for tag with the Slug of 'extreme' is being displayed.
is_tag( 'extreme' );

// When the archive page for tag with the Name of 'mild' is being displayed.
is_tag( 'mild' );

 * Returns true when the tag of posts being displayed is either term_ID 30,
 * or slug "extreme", or name "mild". Note: the array ability was added
 * at Version 3.7.
is_tag( array( 30, 'mild', 'extreme' ) );

Get tag description by ID in WordPress

If you want to display the WordPress tag description by ID use the tag_description() function.

echo tag_description( $tag_id );

You can write a description of your tag in the tags menu this description is one of the sections that are important for WordPress SEO. Some plugins like Yoast expands this description for users so that they can write a good description for the tag in WordPress.