Title: タームのリンクの取得について
Last modified: 2017年1月29日

---

# タームのリンクの取得について

 *  [yuura](https://ja.wordpress.org/support/users/yuura/)
 * (@yuura)
 * [10年、 4ヶ月前](https://ja.wordpress.org/support/topic/%e3%82%bf%e3%83%bc%e3%83%a0%e3%81%ae%e3%83%aa%e3%83%b3%e3%82%af%e3%81%ae%e5%8f%96%e5%be%97%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/)
 * 望んでいる結果は得られているのですが、これは違うだろうな…と思っているので質問
   させて下さい。
    [関数リファレンス/get the terms](https://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/get_the_terms)
 * このページを見て、各記事が持っているタームの情報を取得しています。
    ２箇所、それぞれ
   表示したいcssが違うため、見た感じからして無理矢理感がありますが、下記のように
   書いています。
 * front-page.php用
 *     ```
       function custom_taxonomies_term_link(){
         $post = get_post( $post->ID );
         $post_type = $post->post_type;
         $taxonomies = get_object_taxonomies( $post_type, 'objects' );
         $out = array();
         foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
           $terms = get_the_terms( $post->ID, $taxonomy_slug );
           if ( !empty( $terms ) ) {
             $out[] = "";
             foreach ( $terms as $term ) {
               $out[0] =
               '<span class="cat tc catdef '
               . $term->slug
               . '"><a href="'
               . get_term_link( $term->slug, $taxonomy_slug ) .'">'
               . $term->name
               . "</a></span>";
             }
             $out[] = "";
           }
         }
         return implode('', $out );
       }
       ```
   
 * archive.php用
 *     ```
       function custom_taxonomies_terms_links(){
         $post = get_post( $post->ID );
         $post_type = $post->post_type;
         $taxonomies = get_object_taxonomies( $post_type, 'objects' );
         $out = array();
         foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
           $terms = get_the_terms( $post->ID, $taxonomy_slug );
           if ( !empty( $terms ) ) {
             $out[] = "";
             foreach ( $terms as $term ) {
               $out[] =
               '<span class="ichiran-cat tr '
               . $term->slug
               . '"><a href="'
               . get_term_link( $term->slug, $taxonomy_slug ) .'">['
               . $term->name
               . "]</a></span>";
             }
             $out[] = "";
           }
         }
         return implode('', $out );
       }
       ```
   
 * front-page.phpには、複数タームがあるなかの、１つだけ表示させたかったので、
 *     ```
       $out[0] =
       ```
   
 * とゼロを放り込んでみたのですが…間違ってるだろうな…と。
    出力側の
 *     ```
       <?php echo custom_taxonomies_terms_links(); ?>
       ```
   
 * に、通常カテゴリーを１つだけ表示させる、
 *     ```
       <?php $cat = get_the_category(); $cat = $cat[0]; { echo '<a href="' . get_category_link($cat->term_id) . '" title="'. esc_attr($cat->name) . '">' . $cat->cat_name . '</a>'; } ?>
       ```
   
 * のようにかければ、functions.phpに書くのは１つで済むのになぁとまでは考えている
   のですが…。
 * どうぞよろしくお願いいたします！

1件の返信を表示中 - 1 - 1件目 (全1件中)

 *  [jim912](https://ja.wordpress.org/support/users/jim912/)
 * (@jim912)
 * [10年、 4ヶ月前](https://ja.wordpress.org/support/topic/%e3%82%bf%e3%83%bc%e3%83%a0%e3%81%ae%e3%83%aa%e3%83%b3%e3%82%af%e3%81%ae%e5%8f%96%e5%be%97%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/#post-162050)
 * > front-page.phpには、複数タームがあるなかの、１つだけ表示させたかったので、
   >     ```
   >     $out[0] =
   >     ```
   > 
   > とゼロを放り込んでみたのですが…間違ってるだろうな…と。
 * このコードだと、取得した複数タームのうち最後のタームが採用されます。理由はおわか
   りになるでしょうか。
 * > 通常カテゴリーを１つだけ表示させる、
   >     ```
   >     <?php $cat = get_the_category(); $cat = $cat[0]; { echo '<a href="' . get_category_link($cat->term_id) . '" title="'. esc_attr($cat->name) . '">' . $cat->cat_name . '</a>'; } ?>
   >     ```
   > 
   > のようにかければ、functions.phpに書くのは１つで済むのになぁとまでは考えている
   > のですが…。
 * いくつか方法は考えられると思います。
    - 関数内で条件分岐タグを使って、分岐処理させる
    - 関数の引数によって、class の属性値、出力数を変化させる
 * なお、関数冒頭にある
 *     ```
       $post = get_post( $post->ID );
       ```
   
 * において、$post->ID は取得できていないはずですので、引数を指定する必要はありま
   せん。

1件の返信を表示中 - 1 - 1件目 (全1件中)

 トピック「タームのリンクの取得について」には新たに返信することはできません。

## タグ

 * [カスタム投稿](https://ja.wordpress.org/support/topic-tag/%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e6%8a%95%e7%a8%bf/)
 * [タクソノミー](https://ja.wordpress.org/support/topic-tag/%e3%82%bf%e3%82%af%e3%82%bd%e3%83%8e%e3%83%9f%e3%83%bc/)
 * [ターム](https://ja.wordpress.org/support/topic-tag/%e3%82%bf%e3%83%bc%e3%83%a0/)

 * フォーラム: [使い方全般](https://ja.wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1件の返信
 * 2人の参加者
 * 最後の返信者: [jim912](https://ja.wordpress.org/support/users/jim912/)
 * 最後の活動: [10年、 4ヶ月前](https://ja.wordpress.org/support/topic/%e3%82%bf%e3%83%bc%e3%83%a0%e3%81%ae%e3%83%aa%e3%83%b3%e3%82%af%e3%81%ae%e5%8f%96%e5%be%97%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/#post-162050)
 * 状態: 未解決

## トピック

### 人気のトピック

### 返信がないトピック

### サポート関連ではないトピック

### 解決済みのトピック

### 未解決のトピック

### すべてのトピック
