When viewing a tag page, is it possible to somehow output (within the loop) the categories that tag is featured in rather than listing it’s posts?
The look in my homepage loops is as follows, im looking for the same kind of thing but on the tag page.
<ul class="thumb-grid">
<?php query_posts('meta_key=is_front_page&posts_per_page=5&paged=$paged'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a href="<?php bloginfo('url'); ?>/movies/<?php $category = get_the_category(); echo $category[0]->category_nicename; ?>">
<img src="<?php echo $my_image_url = get('movie_thumbnail'); ?>" alt="" />
<span class="title"><?php $category = get_the_category(); echo $category[0]->cat_name; ?></span>
<span class="feat"><?php $articletags = strip_tags(get_the_tag_list('',', ',''));echo $articletags;?></span>
</a>
</li>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
</ul>
This is a solution that outputs a list of categories that contain posts sharing the current tag once on the page, not within the loop. I only tested it on a tag page, where the ‘tag’ query var is available. It makes use of custom queries using the
$wpdb
class. (This can be very useful to learn).You need to define your own table prefix at the beginning, or set as
''
.Testing
I tested this code by placing it directly in tag.php. It would be smarter to place it in your own plugin or theme’s functions.php. Some tweaking may be necessary to make sure the $wp_query object is available inside that function.
Terms and Taxonomy
Remember is that tags and categories are considered equal in the wordpress database. The same way posts, pages,revisions, and drafts are all ‘posts’, categories and tags are ‘terms’. The distinction between them is their ‘taxonomy’ which is simply whether the term is a ‘category’ or ‘tag’.
The ‘terms’ table holds all tags and categories. The ‘term_taxonomy’ stores whether the term is a tag,category, or post link. The ‘term_relationships’ table pairs posts and taxonomies. The ‘object_id’ column can hold post id’s or term id’s in this table.
Code Outline
wp_list_categories()
with the category_id string as the categories to include.Code Example
I hope this works for you.
UPDATE
In response to your comment, to get categories and descriptions and other cat meta data, you can replace
wp_list_categories
withget_categories
while still passing theinclude='.$category_ids
. This will return an array of category objects that you can loop through as you wish and display the category, description, post count and more.New Code Example (From the codex page)