开发者

Display the tag categories

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 clas开发者_StackOverflows="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

  1. Sets the table prefix
  2. Gets the tag name from the query_vars
  3. Gets the tag's term_id from the terms table
  4. Gets the post id's (named 'object_id') from the term_relationships table as a numerically indexed array
  5. Turns that numeric array into a comma-separated string
  6. Trims the trailing comma
  7. Selects all term_taxonomy_id's (categories AND tags) that have the post id's from #4 from term_relationships as a multi-dimensional associative array
  8. Create an array of terms in all the posts having this tag.
  9. Foreach element in this array, select the taxonomy (whether it is a 'category' or a 'tag') and if it is a category, put the term_id into an array.
    1. Make that array into a string for use in the next step.
    2. call wp_list_categories() with the category_id string as the categories to include.

Code Example

//define table prefix
$wp_pre = 'wp_';
//get tag name from url
$tag_name = $wp_query->get('tag');
//get term_id from database
$term_id = $wpdb->get_var("
     SELECT term_id
     FROM ".$wp_pre."terms
     WHERE name ='".$tag_name."'");

//select post id's with this tag
$posts_with_tag = $wpdb->get_results("
      SELECT object_id
      FROM ".$wp_pre."term_relationships
      WHERE term_taxonomy_id = '".$term_id."'",
      ARRAY_N);

//make string out of returned array in an array
 $posts_with_tag_as_string = implode(',',$posts_with_tag[0]);

//trim trailing comma
$posts_with_tag_as_string = rtrim($posts_with_tag_as_string,',');
//select all terms having post id
$terms_having_post = $wpdb->get_results("
      SELECT term_taxonomy_id
      FROM ".$wp_pre."term_relationships
      WHERE object_id
      IN ('".$posts_with_tag_as_string."')",
      ARRAY_A);

 foreach($terms_having_post as $key=>$val){
      $post_terms[] = $val['term_taxonomy_id'];
 }

 //get taxonomy name for each term_having_post    
 foreach($post_terms as $term_id){
      $taxonomy = $wpdb->get_var("
           SELECT taxonomy
           FROM ".$wp_pre."term_taxonomy
           WHERE term_id = '".$term_id."'");

      if($taxonomy == 'category'){
          $cats[] = $term_id;
      }
 }

 $category_ids = implode(',',$cats);
 //trim trailing comma
 $category_ids = rtrim($category_ids,',');

 //output list of categories with the included id's.
 wp_list_categories('include='.$category_ids);

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 with get_categories while still passing the include='.$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)

$args=array(
  'orderby' => 'name',
  'order' => 'ASC'
  );

$categories=get_categories($args);
foreach($categories as $category) { 
    echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    echo '<p> Description:'. $category->description . '</p>';
    echo '<p> Post Count: '. $category->count . '</p>';  
 } 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜