2017-05-07 12 views
1

私は分類スラグを取得するには、次のコードを使用しています:フェッチ分類スラッグ

<?php 
    $terms = get_the_terms($post->ID, 'locations'); 
    if (!empty($terms)){ 
     $term = array_shift($terms); 
    } 
?> 

私は、出力にスラグ次のコードを使用しています:私の質問はどのようにすることができ、ある

<?php echo $term->slug; ?> 

を同じ場所に2つの異なるタクソノミーを出力するためにこれを使用しますか?たとえば、

<?php 
    $terms = get_the_terms($post->ID, 'locations', 'status'); 
    if (!empty($terms)){ 
     $term = array_shift($terms); 
    } 
?> 

「場所」、「ステータス」という用語を追加することはできますが、機能しません。

答えて

0

2つ以上のタクソノミーを表示したい場合は、$ terms変数をループする必要があります。

<?php 
    $terms = get_the_terms($post->ID, 'locations'); 
    if (!empty($terms)){ 
     foreach ($terms as $term): 
      echo $term->slug; 
     endforeach; 
    } 
?> 

は、そのあなたのお役に立てば幸いです。

+0

感謝。私はそれを明確にするために私の答えを更新しました。私は上記のコードを使用して2つの異なる分類を出力しようとしています。 – CharlyAnderson

+0

それは本当にあなたが出力したいものに依存していますか? –

+0

分類法のスラグを出力したい。 – CharlyAnderson

0

get_the_termsための公式ドキュメントによると、唯一の分類を供給することができありがとうございました。 2つの異なるタクソノミー内のすべての用語のスラッグを出力したい場合は、Mohammadが提案したように2回行うことができます。

すなわち

<?php 

// output all slugs for the locations taxonomy 
$locations_terms = get_the_terms($post->ID, 'locations'); 
if (! empty($locations_terms)) { 
    foreach ($locations_terms as $term) { 
     echo $term->slug; 
    } 
} 

// output all slugs for the status taxonomy 
$status_terms = get_the_terms($post->ID, 'status'); 
if (! empty($status_terms)) { 
    foreach ($status_terms as $term) { 
     echo $term->slug; 
    } 
} 
?> 

あなただけのタクソノミーのそれぞれの個々の用語のスラグを得るために気場合は、あなたがget_term_byがより便利かもしれません。コメントのため

すなわち

<?php 
$loc_field = 'name'; 
$loc_field_value = 'special location'; 
$loc_taxonomy = 'locations'; 
$locations_term = get_term_by($loc_field, $loc_field_value, $loc_taxonomy); 
echo $locations_term->slug; 

$stat_field = 'name'; 
$stat_field_value = 'special status'; 
$stat_taxonomy = 'status'; 
$status_term = get_term_by($stat_field, $stat_field_value, $stat_taxonomy); 
echo $status_term->slug; 
?>