I’m looking for a way to list all the tags associated with the current post as a bulleted list in the sidebar, ideally with a CSS class for custom styling. All the plugins and code snippets I’ve found either display all tags site-wide, or display it as a tag cloud. My hunch is that it would involve calling get_the_tag_list outside of the loop somehow, but I’m a complete novice to WordPress development and am not sure how to get that working without guidance.

Any help would be greatly appreciated!

Thanks so much in advance,

Julian

1 Answer
1

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.

Tags:

Leave a Reply

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