phpwordpress

How do I get all the post tags in WordPress?


I would like to get all the post tags in my WordPress. Below is my code in the footer:

<?php
global $wpdb;

$tags = get_terms('post_tag');
echo '<ul>';
foreach ($tags as $tag)
{
    echo '<li>' . $tag->name . '</li>';
}
echo '</ul>';
?>

With the above code I am getting only the tags associated with a specific post, not the entire list of tags in WordPress.

Any help will be appreciated. Thanks.


Solution

  • Use get_tags to get all posts tags

    <?php 
    $tags = get_tags(array(
      'hide_empty' => false
    ));
    echo '<ul>';
    foreach ($tags as $tag) {
      echo '<li>' . $tag->name . '</li>';
    }
    echo '</ul>';
    ?>