Display tags for current post in sidebar

You can use get_the_tag_list(), you just need to set the 4th argument, $id to get_queried_object_id() which gets the ID of the main queried post/page outside of the loop. You’ll want to check is_singlar() too though, in case the queried object is a tag/category with the same ID as a post:

<?php
if ( is_singular() ) :
    echo get_the_tag_list(
        '<ul class="my-tags-list"><li>',
        '</li><li>',
        '</li></ul>',
        get_queried_object_id()
    );
endif;
?>

The first 3 arguments are the HTML before the list, separating each list item, and after the list. The configuration I have there wraps the whole thing in an unordered list and all items in list item tags. The list has the class my-tags-list that can be used for styling. You can change that to whatever you want.

Leave a Comment