I’m trying to get this less manual than it currently is. I want to display tags that appear in only a single category, so if they are in a post that has two categories, I want that tag ignored.
The following code works, but is a little clunky. Ideally I’d like to be able to skip having to add the excludes and just have it figure it out itself.
<?php
function pl8_artist_list($catname, $exclude) {
$custom_query = new WP_Query('posts_per_page=-1&category_name='.$catname.'&cat='.$exclude.'');
if ($custom_query->have_posts()) :
while ($custom_query->have_posts()) : $custom_query->the_post();
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo '<h2>' . $tag->name . '</h2>';
echo '<p>' . $tag->description . '</p>';
echo '<p><a href="http://beatexplorers.com/artist/'. $tag->slug . '">Read posts about ' . $tag->name . '</a></p>';
}
}
endwhile;
wp_reset_postdata(); // reset the query
endif;
}
?>
And I call it like so:
<?php
pl8_artist_list(trance, -3,-11,-4,-8);
?>
The excludes being the other categories that the tags may appear in.
I’m quite new to PHP, so any light that could be shed would be greatly appreciated.
This loop starts by fetching all posts in given category. then goes through each tag of current post, fetches the posts for each tag, and if all of the posts have 0 or 1 category, prints that category. So, if you have lots of tags, lots of posts, this could be slow-ish.