显示标签类别

时间:2010-11-09 10:21:04

标签: wordpress

在查看标签页时,是否可以以某种方式输出(在循环内)标签所属的类别而不是列出其帖子?

我的主页循环中的外观如下所示,我在标签页面上寻找相同类型的东西。

            <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>

1 个答案:

答案 0 :(得分:0)

这是一个解决方案,输出一个类别列表,其中包含在页面上共享当前标记的帖子,而不是在循环内。我只在标签页面上测试过,其中'tag'查询var可用。它使用自定义查询using the $wpdb class。 (这对学习非常有用)。

您需要在开头定义自己的表格前缀,或设置为''

<强>测试 我通过直接将它放在tag.php中测试了这段代码。将它放在你自己的插件或主题的functions.php中会更聪明。可能需要进行一些调整以确保$ wp_query对象在该函数内可用。

条款和分类 请记住,wordpress数据库中的标签和类别被认为是相同的。帖子,页面,修订和草稿的所有“帖子”,类别和标签都是“条款”。它们之间的区别在于它们的“分类学”,即术语是“类别”还是“标记”。

'terms'表包含所有标签和类别。 'term_taxonomy'存储该术语是标签,类别还是后期链接。 'term_relationships'表对帖子和分类法进行配对。 'object_id'列可以保存此表中的帖子ID或术语ID。

代码大纲

  1. 设置表前缀
  2. 从query_vars
  3. 获取标记名称
  4. 从术语表
  5. 中获取标记的term_id
  6. 从term_relationships表中获取post id(名为'object_id')作为数字索引数组
  7. 将数字数组转换为以逗号分隔的字符串
  8. 修剪尾随逗号
  9. 从term_relationships中选择所有term_taxonomy_id(类别和标签),其中#4的帖子ID为多维关联数组
  10. 在具有此标记的所有帖子中创建一个术语数组。
  11. 此数组中的Foreach元素,选择分类法(无论是“类别”还是“标记”),如果是类别,请将term_id放入数组中。
    1. 将该数组转换为字符串,以便在下一步中使用。
    2. 使用category_id字符串作为要包含的类别调用wp_list_categories()
  12. 代码示例

    //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);
    

    我希望这适合你。

    <强>更新 在回复您的评论,get categories以及说明和其他cat元数据时,您仍可以wp_list_categories替换get_categories,同时仍然传递include='.$category_ids。这将返回一个类别对象数组,您可以根据需要循环,并显示类别,描述,发布计数等。

    新代码示例(来自法典页面)

    $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>';  
     } 
    
相关问题