2017-11-08 9 views
1

私は特定の属性タクソノミーから用語slugを取得しようとしていますが、何も得られません。特定の属性タクソミーの用語を取得するslug

$attr_langs = 'pa_languages'; 
$attr_langs_meta = get_post_meta('id', 'attribute_'.$attr_langs, true); 
$term = get_term_by('slug', $attr_langs_meta, $attr_langs); 

ありがとうございます!

答えて

0

それは(ポストID)'id'としてテキスト文字列ないで、それはget_post_meta()最初の関数として数値にする必要がある引数は機能しないことが普通ですが、...
だから、あなたは「できます変数$attr_langs_metaの任意の値を取得します。

1)WP_Postオブジェクトからで:

global $post; 
$product_id = $post->ID; 

2)WC_Productオブジェクトからで:

$product_id = $product->get_id(); 
あなたはさまざまな方法でプロダクトIDを取得することができます

正しい方法は次のとおりです。

変数製品(同様のこと)のために
// The defined product attribute taxonomy 
$taxonomy = 'pa_languages'; 

// Get an instance of the WC_Product Object (necessary if you don't have it) 
// from a dynamic $product_id (or defined $product_id) 
$product = wc_get_product($product_id); 

// Iterating through the product attributes 
foreach($product->get_attributes() as $attribute){ 
    // Targeting the defined attribute 
    if($attribute->get_name() == $taxonomy){ 
     // Iterating through term IDs for this attribute (set for this product) 
     foreach($attribute->get_options() as $term_id){ 
      // Get the term slug 
      $term_slug = get_term($term_id, $taxonomy)->slug; 

      // Testing an output 
      echo 'Term Slug: '. $term_slug .'<br>'; 
     } 
    } 
} 

、あるいはまた、もっぱら:

// The defined product attribute taxonomy 
$taxonomy = 'pa_languages'; 

// Get an instance of the WC_Product Object (necessary if you don't have it) 
// from a dynamic $product_id (or defined $product_id) 
$product = wc_get_product($product_id); 

// Iterating through the variable product variations attributes 
foreach ($product->get_variation_attributes() as $attribute => $terms) { 
    // Targeting the defined attribute 
    if($attribute == $taxonomy){ 
     // Iterating through term slugs for this attribute (set for this product) 
     foreach($terms as $term_slug){ 
      // Testing an output 
      echo 'Term Slug: ' . $term_slug . '<br>'; 
     } 
    } 
} 

これはあなたのために働く必要があります...

+0

は答えてくれてありがとう、私は、製品が可変で言及したいと思います私は '$ product_id = $ product-> get_id();'を使って動的にする必要があったので、あらかじめ定義することはできませんが、このスクリプトの回避策は簡単ですか? –

+0

@WiTonNope定義済みのプロダクトIDは単なる例に過ぎません。もちろん、動的であってもかまいません。 'WC_Product'オブジェクト' $ product; 'を直接使用することができます。コードをどこで使用しているかによって異なります...このコードをどこで使用しているのか(関数内、テンプレート内)、あなたの質問のコードを完成させ、いくつかの詳細を追加するかもしれません... – LoicTheAztec

関連する問題