A Brief Tutorial on WordPress Tags

Tags are the most important part of WordPress that is useful for search engines and better user experience for moving easily to the related topics. One point to note that we can only use tags inside WordPress post loop, outside the loop it would not work. Here is a brief tutorial regarding WordPress tags.

 

1. the_tags()

Syntax:

[code type=”php”]<?php the_tags($before_tag, $separator, $after_tag); ?>[/code]

$before_tag (Optional): It is used to display text you want to display before your tags. You can put it anything here like, cloud tags, tags etc. You can define HTML starting elements here.

$separator (Optional): Separator is used to fill the gap between two and more tags, if one post has many tags. You can use any type of separator of your choice. For example, -,>,< etc. Default separator is ‘-‘.

$after_tag (Optional): It works like $before_tag. You can define ending HTML elements here.

Example 1:

[code type=”php”]<?php the_tags(); ?>[/code]

Output: WordPress Tutorials, WordPress

Example 2:

[code type=”php”]<?php the_tags(‘My Tags’, ‘>>’, ”); ?> [/code]

Output: WordPress Tutorials >> WordPress

2. tag_description()

This function is used to get description of tag using post id. But using of post id is optional, because if you will not use, by default it will take post id according to post loop as we all know use, we can use tags inside a post loop.

Syntax:

[code type=”php”]<?php tag_description($your_post_id); ?>[/code]

Examples:

[code type=”php”]<?php tag_description(‘5’) ?>[/code]

Or

[code type=”php”]<?php tag_description($post->ID) ?>[/code]

Or

[code type=”php”]<?php echo ‘Read Tag Description’ . tag_description(‘5’) ?>[/code]

3. single_tag_title()

This function will return the tag title of your current page, post or archive page. For example, if user will come on archive page, you can set it there like “Your are currently Browsing Archive Page” and same with other post types.

Syntax:

[code type=”php”]<?php single_tag_title($your_text_or_anything_as_prefix, $display_true_or_false); ?>[/code]

Examples:

[code type=”php”]<?php single_tag_title(‘You are visiting Archive Page’); ?>[/code]

[code type=”php”]<?php $get_curr_tag = single_tag_title(”, false); ?>[/code]

4. wp_tag_cloud()

This function is used to show a list of all tags and you will notice difference between size of many tags. If any tag has larger size, it means this tag is assigned to many posts as compared to other tags. Every parameters which I mention below are optional.

Syntax:

[code type=”php”]<?php wp_tag_cloud($params); ?>[/code]

Where

[code type=”php”]
<?php $parmas = array(
‘largest’ => 22, //Displays the most used tags in 22 size.
‘smallest’ => 3, //Display the less used tags in 3 size.
‘unit’ => ‘px’, //Define the unite of size, you can em, pt etc.
‘number’ => ’30’, //Define how many tags you want to display in your cloud tag.
‘separator’ => ‘,’, //Separator between tags.
‘orderby’ => ‘name’, //Sort the tags by name.
‘order’ => ‘ASC’, //ASC for ascending order, DESC for descending order
‘exclude’ => ‘null’, //Specify no tags should be excluded.
‘include’ => ‘null’, //Include all the tags.
‘link’ => ‘view’, //How user will find the tags
‘taxonomy’ => ‘post_tag’,//Show tags on the basis of posts
‘echo’ => true //Echo all the results
); ?>
[/code]

5. get_tags()

This function is for retrieving an array of object of each term on the basis of parameters we pass. Every parameter mentions below is optional.

Syntax:

[code type=”php”]<?php get_tags($params); ?>[/code]

Where

[code type=”php”]
<?php
$params = array(
‘orderby’ => ‘name’,
‘order’ => ‘DESC’,
‘hide_empty’ => true, //0 counted terms will not return. You can choose false.
‘exclude’ => ‘null’,
‘include’ => ‘null’,
‘number’ => ‘5’, //5 terms will return.
‘fields’ => ‘all’,
‘slug’ => ‘null’,
‘hierarchical’ => true,
); ?>
[/code]

6. get_tag()

In get_tag(), it retrieves tag of a post by id or object. First parameter is required, the other two are optional.

Syntax:

[code type=”php”]<?php &get_tag($tagId, $result, $filter); ?>[/code]

7. is_tag()

This conditional function is used to check a specific tag is being displayed on archive page or not. is_tag only returns true or false. The parameter is optional.

Syntax:

[code type=”php”]<?php is_tag($slug); ?>[/code]

Example:

[code type=”php”]
<?php if(is_tag()) {
echo ‘Found’;
else
echo ‘Not Found’;
[/code]

8. get_the_tag_list()

It generates a full list of tags of the post and each tag will be attached to its relavent page. You can use get_the_tag_list() only inside the loop.

Syntax:

[code type=”php”]
<?php $posttags = get_the_tag_list($before_tag, $sep, $after_tag);
[/code]

Example

[code type=”php”]
<?php echo get_the_tag_list(‘<div>Tags: ‘,’, ‘, ‘</div>’); ?>
[/code]

Output

[code type=”php”]
<div>Tags:
<a href=”tag_link1″>Tag Text 1</a>,
<a href=”tag_link2″>Tag Text 2</a>,

</div>
[/code]

Duan Lingxin

Duan Lingxin

Content crafter and chief editor at Scratching Info. Also regular contributor on other major online tech platforms. Security Specialist by day and a writer by night, he does his best to instill his knowledge about tech while delivering inspiring and life changing resources through his writing,

More Posts - Website

Leave a Comment