X

How to Show All Tags Related to a Category in WordPress

Snippets by IsItWP

Want to show all tags related to a category in WordPress? Showing all related tags can help users find relevant content that they might be interested in. In this article, we’ll show you how to show all tags related to a category on your WordPress site.

Instructions:

All you have to do is add this code to your template file where you want the tags to be displayed. If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

<?php
$project_query = query_posts('category_name=Real estate marketing');
while (have_posts()) : the_post();
    $posttags = get_the_tags();
    if ($posttags) {
        foreach($posttags as $tag) {
            $all_tags_arr[] = $tag -> name; //USING JUST $tag MAKING $all_tags_arr A MULTI-DIMENSIONAL ARRAY, WHICH DOES WORK WITH array_unique
        }
    }
endwhile;

$tags_arr = array_unique($all_tags_arr); //REMOVES DUPLICATES
foreach( $tags_arr as $tag ):
    $el = get_term_by('name', $tag, 'post_tag');
    $arr[] = '"tag-'.$el->slug.'"';
?>
&lt;span>&lt;a href="<?php echo bloginfo('url'); ?>/tag/<?php echo $el->slug; ?>" id="taglink-tag-<?php echo $el->slug; ?>" rel="tag-<?php echo $el->slug; ?>"><?php echo $el->name; ?>&lt;/a> &lt;span class="slash">, &lt;/span>&lt;/span>
<?php endforeach; ?>

If you liked this code snippet, please consider checking out our other articles on the site like: 62 best free WordPress blog themes or 7 best WordPress contact form plugins.

Comments   Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!