get the terms

本文解决的问题:

获取文章的taxonomy

Get terms for all custom taxonomies

place this function in theme functions.php

<?php // get taxonomies terms links
function custom_taxonomies_terms_links() {
   global $post, $post_id;
   // get post by post id
   $post = &get_post($post->ID);
   // get post type by post
   $post_type = $post->post_type;
   // get post type taxonomies
   $taxonomies = get_object_taxonomies($post_type);
   $return = '';
   foreach ($taxonomies as $taxonomy) {
      // get the terms related to post
      $terms = get_the_terms( $post->ID, $taxonomy );
      if ( !empty( $terms ) ) {
         $out = array();
         foreach ( $terms as $term )
            $out[] = '<a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a>';
         $return .= join( ', ', $out );
         $return .= ',';
      }
   }
   return $return;
}?>

Now you can use this function in your themes for your post types without the need to input anything

<?php echo custom_taxonomies_terms_links(); ?>

原文地址:
http://codex.wordpress.org.cn/Function_Reference/get_the_terms#A_Basic_Example
其他相关:
http://codex.wordpress.org.cn/Function_Reference/the_terms
欢迎评论。
lingkb » get the terms

发表评论