2016-08-22 71 views
4

我不希望使用任何插件來完成此任務,因爲我最近與類似項目發生衝突,導致網站崩潰。所以我希望從基礎知識中創建這個功能。WooCommerce品牌分類術語列表中的下拉選擇器

我需要產品類別頁面上的下拉列表來按品牌選擇產品。下拉列表將顯示所有品牌。當您選擇一個網站時,網站只會顯示分配給該品牌的產品。我們並不需要使用允許通過新奇,價格,人氣等

查看使用我已經建立了我的品牌和分配每一個產品一個品牌 WooCommerce 'Brands'分類內置下拉。

我可以查看的所有品牌的陣列以及它們與下面的代碼屬性:

$brands = get_terms('brand'); 
print_r($brands); 

哪個輸出以下:

陣列(

[0] => WP_Term對象([term_id] => 978 [name] => Imari Sometsuke [slug] => imari-sometsuke [term_group] => 0 [term_taxonomy_id] => 978 [taxonomy] => brand [description] => [parent] = > 0 [count] => 1 [filter] => raw)

[1] => WP_Term對象([term_id] => 982 [name] => Kutani [slug] => kutani [term_group] => 0 [term_taxonomy_id] => 982 [taxonomy] => brand [description ] => [parent] => 0 [count] => 2 [filter] => raw)

[2] => WP_Term對象([term_id] => 977 [name] => Kutani Shoza [slug] => kutani-shoza [term_group] => 0 [term_taxonomy_id] => 977 [taxonomy] => brand [description] => [parent] => 0 [count] => 4 [filter] => raw)

[3] => WP_Term對象([term_id] => 979 [name] => Kutani Tokkuri [slug] => kutani-tokkuri [term_group] => 0 [term_taxonomy_id] => 979 [taxonomy] => brand [description ] => [parent] => 0 [count] => 2 [filter] => raw)

[5] => WP_Term對象([term_id] => 985 [name] => Nishikawa Sukenobu [slug] => nishikawa-sukenobu [term_group] => 0 [term_taxonomy_id] => 985 [taxonomy] =>品牌[description] => [parent] => 0 [count] => 1 [filter] => raw)

[6] => WP_Term Object([term_id] => 984 [name] => [slug] => shinsui-ito [term_group] => 0 [term_taxonomy_id] => 984 [taxonomy] => brand [description] => [parent] => 0 [count] => 2 [filter] => raw)

=> WP_Term對象([term_id] => 976 [name] => Takeji Asano [slug] => takeji-asano [term_group] => 0 [term_taxonomy_id] => 976 [taxonomy] => brand [description] => [parent] => 0 [count] => 2 [filter] => raw)

[8] => WP_Term對象([term_id] => 980 [name] => Toshusai Sharaku [slug] => toshusai-sharaku [term_group] => 0 [term_taxonomy_id] => 980 [分類法] =>品牌[描述] => [父] => 0 [數] => 3 [過濾] =>原始)

一個會如何構建的下拉(選擇)列表來創建這個? 我想也是的框架是這樣的,我已經開始:

<?php 
$brands = get_terms('brand'); 
//print_r($brands); 
?> 

<select name="orderby" class="orderby"> 
    <?php foreach ($brands as ???) : ?> 
     <option value="<?php echo esc_attr($???); ?>" <?php selected($orderby, $???); ?>><?php echo esc_html($???); ?></option> 
    <?php endforeach; ?> 
</select> 

回答

2

正如你可以看到它的長期目標(WP_Term對象)的數組,你必須使用對象語法對於這個詞的每個屬性,在循環中,這種方式:

<?php 

    $brands = get_terms('brand', array(
     'orderby' => 'name' // orderby arguments ('name', 'slug','term_group', 'term_id', 'id', 'description') 
    )); 
    //print_r($brands); 

?> 
<select name="orderby" class="orderby"> 
    <?php 
     foreach ($brands as $key => $brand) : 
      $brand_id = $brand->term_id; 
      $brand_name = $brand->name; 
      $brand_slug = $brand->slug; 
      $brand_term_group = $brand->term_group; 
      $brand_term_taxonomy = $brand->term_taxonomy_id; 
      $brand_taxonomy = $brand->taxonomy; 
      $brand_description = $brand->description; 
      $brand_parent = $brand->parent; 
      $brand_count = $brand->count; 
      $brand_filter = $brand->filter; 

      $number = $key+1; 
      $option = 'option-' . $number; 
    ?> 
     <option value="<?php echo $option; ?>"><?php echo $brand->name; ?></option> 
    <?php endforeach; ?> 
</select> 

該代碼測試和工程