WordPress.org

Forums

How To Display Tags From Certain Categories (1 post)

  1. Ryan
    Member
    Posted 1 year ago #

    Just spent about 2-3 hours trying to figure this out, so I thought I would post it to save time for the next unfortunate person to have to do it.

    My problem was that I had a blog page and wanted to display all the tags for the posts on that page in the sidebar, but I only wanted to list tags from the category that the blog posts were in ('News' in my case). After way too much searching and trying a plugin that broke my site, I came up with this nice, compact solution:

    $tags = new WP_Query('showposts=999&cat=8');
    			while ($tags->have_posts()) : $tags->the_post();
    			foreach ((wp_get_post_tags(get_the_ID(), array('fields' => 'ids'))) as $tagid) {
    				$whichtags .= $tagid . ',';
    			}
    			endwhile;
    
    			echo "<div class='tagcloud'><h1>tags</h1>";
    			wp_tag_cloud("include=" . $whichtags);
    			echo "</div>";

    Simply change the 'cat' argument in the $tags query to whatever category ID you need.

    Hope this saves some time for others who run into this issue.

Topic Closed

This topic has been closed to new replies.

About this Topic