2016-11-14 119 views
5

我一直在研究關於我的問題的所有網絡和論壇,但我似乎無法產生正確的結果。基本上我試圖僅顯示特定產品類別的條款或產品屬性。顯示特定產品類別的條款或產品屬性(woocommerce)

這是我一直在努力的代碼。

<fieldset class="liquor-types-control filter-controls" > 

    <?php 

    $terms = get_terms('wine', /*<--Product Category */ 'pa_liquor-types' /* <--Product Attribute */); 
    foreach ($terms as $term) : 
    ?> 

    <label> 
     <input type="checkbox" value="<?php echo "." . $term->slug ?>" /> 
     <?php echo $term->name ?> 
    </label> 

    <?php endforeach; ?> 


</fieldset> 


(
    [errors] => Array 
     (
      [invalid_taxonomy] => Array 
       (
        [0] => Invalid taxonomy. 
       ) 

     ) 

    [error_data] => Array 
     (
     ) 
) 
+0

你可以共享'的var_dump($計算)',所以我們可以看到數據的格式?另外,你會得到什麼輸出?任何錯誤?確保設置error_reporting(E_ALL); – mseifert

+0

用更多的細節更新了我的問題 – clestcruz

回答

3

使用;在$ term-> slug和$ term->名字之後。

<label> 
     <input type="checkbox" value="<?php echo $term->slug; ?>" /> 
     <?php echo $term->name; ?> 
    </label> 
+0

我認爲這不會起作用 – clestcruz

+0

請檢查您的代碼,您的代碼有錯誤。聲明應該以;以及爲什麼你使用<?php echo「。」 。 $ term-> slug?>。這是不正確的。 –

+0

並且get_terms('wine');足夠。 –

3

嘗試這樣:

<?php 
$terms = get_terms('wine', 'pa_liquor-types'); 

foreach($terms as $term) { ?> 
    <input type="checkbox" value="<?php echo "." . $term['slug'];?>"/> 
    <?php echo $term['name'];?> 
<?php } ?> 
+0

試過你的解決方案,但沒有運氣 – clestcruz

4

var_dump是顯示您正在使用WordPress的分類法。雖然我沒有直接與WordPress的經驗,the Wordpress site docs說:

此前4.5.0,get_terms的第一個參數()是一個分類或分類的 名單:

由於4.5.0 ,分類應通過 '分類' 的說法 $ args數組中傳遞:

From the function reference:

$term = get_term($term_id, $taxonomy); 

使你術語蛞蝓:例如: - 術語-鼻涕蟲示例

$slug = $term->slug; 

使你術語名稱:例如術語名稱實例

$name = $term->name; 

首先,確保你使用的是正確的版本 - 使用的是從之前4.5.0

二語法,錯誤是說,分類pa_liquor-types是無效的。你需要檢查這個被定義的位置。

檢查您的create_initial_taxonomies()函數語法並在必要時發佈它。

0

分別獲得葡萄酒和白酒類型的條款,將它們合併到單個數組條目中並通過該數組循環以獲得所有條款。希望它有幫助,還沒有測試過代碼。

<fieldset class="liquor-types-control filter-controls" > 

    <?php 
    global $post; 
    $terms_wine = get_the_terms(get_the_ID(),'wine'); 
    $terms_liquor = get_the_terms(get_the_ID,'pa_liquor-types'); 
    $terms = array(); 
    foreach($terms_wine as $wine){ 
     array_push($terms,$wind); 
     } 
    foreach($terms_liquor as $liquor){ 
     array_push($terms,$liquor); 
     } 


    foreach ($terms as $term) : 
    ?> 

    <label> 
     <input type="checkbox" value="<?php echo "." . $term->slug ?>" /> 
     <?php echo $term->name ?> 
    </label> 

    <?php endforeach; ?> 


</fieldset> 
+0

不知道你是否有$發佈或沒有,如果沒有獲得當前產品的條件你想列出和取代$發佈產品的ID。 – Yamu