get_the_termsを汎用的に使用したい
-
よくカスタム分類のslugとnameを取得してテーマに反映しなければいけないことが多く、いつもあまり綺麗なコードがかけず同じようなコードを何度も書いてしまうので、どなたかご教授いただけると幸いです。
同じページ内でcssのクラス名に$term->slugを、カテゴリ名を表示させるために$term->nameを使います。そのためCodexを参考にfunctions.phpで
function cstm_tax_slug() { $post = get_post($post->ID); $post_type = $post->post_type; $taxonomies = get_object_taxonomies($post_type, 'categorys'); foreach($taxonomies as $taxonomy_type => $taxonomy){ $terms = get_the_terms($post->ID, $taxonomy_type); if(!empty($terms)){ foreach($terms as $term) { $terms_slug_out[] = $term->slug; } } return implode('', $terms_slug_out); } }
と書いたのですが、これだとslugしか取得できません。
$term->slugの部分を$term->$termtypeなどにもできず、、、
上記のコードで場所によってslugとnameを切り替えることはできないのでしょうか?よろしくお願いいたします。
2件の返信を表示中 - 1 - 2件目 (全2件中)
2件の返信を表示中 - 1 - 2件目 (全2件中)
- トピック「get_the_termsを汎用的に使用したい」には新たに返信することはできません。