Understand Difference Between get_the_tags() and get_tags() in WordPress – WordPress Tutorial

By | August 1, 2019

WordPress get_the_tags() and get_tags() both can get wordpress post tags, however, there are some differences between them. In this tutorial, we will use some example codes to help you to understand them.

wordpress tags

get the tags():

1) This function is often used in The Loop, If you want to used in other place, you should give a post id.

2) It gets all tags of one post

Here is an example:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ' '; 
}
}
?>
<?php endwhile; endif; ?>

get_tags():

1) This function can be used in anywhere

2) It gets all tags saved in wordpress database,not belong to one post

Here is an example:

<?php
$posttags = get_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ' '; 
}
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *