WordPress:将级别指示器添加到类别列表
WordPress: Add level indicator to category list
我将类别列表显示为 select 字段的选项。
问题是,每个类别看起来都一样。即使它们是子类别。
例如,我的类别树如下所示:
- 主要类别
- 子类别
- 三级类别
但在 select 字段中显示如下:
- 主要类别
- 子类别
- 三级类别
这是我在表单中填充 select 字段的代码:
function populate_dropdown_with_product_categories( $form ) {
//product_cat is the taxonomy for WooCommerce's products
//get the terms for the product_cat taxonomy
$product_categories = get_terms( 'product_cat', array('hide_empty' => false,) );
//Creating drop down item array.
$items = array();
//Adding product category terms to the items array
foreach ( $product_categories as $product_category ) {
$items[] = array( 'value' => $product_category->name, 'text' => $product_category->name );
}
//Adding items to field id 6. Replace 6 with your actual field id. You can get the field id by looking at the input name in the markup.
foreach ( $form['fields'] as &$field ) {
if ( $field->id == 64 ) {
$field->choices = $items;
}
}
return $form;
}
我想我需要在这一行中添加类别的级别:
$items[] = array( 'value' => $product_category->name, 'text' => $product_category->name );
但是我该怎么做呢?
对我来说,如果每个级别在名称前都有一个 -
(第三级两个 -
...)就足够了。
像这样:
- Main category
-- Sub category
--- Third level category
我找到了解决办法。我已经像这样更改了 foreach
代码:
foreach ( $product_categories as $product_category ) {
$product_category_level = count( get_ancestors($product_category->term_id, 'product_cat'));
if ($product_category_level == 1 ) :
$product_category_level_indicator = '- ';
elseif ($product_category_level == 2 ) :
$product_category_level_indicator = '-- ';
else:
$product_category_level_indicator = '';
endif;
$items[] = array( 'value' => $product_category->name, 'text' => $product_category_level_indicator.$product_category->name );
}
对我有用。
感谢反馈。
我将类别列表显示为 select 字段的选项。 问题是,每个类别看起来都一样。即使它们是子类别。
例如,我的类别树如下所示:
- 主要类别
- 子类别
- 三级类别
- 子类别
但在 select 字段中显示如下:
- 主要类别
- 子类别
- 三级类别
这是我在表单中填充 select 字段的代码:
function populate_dropdown_with_product_categories( $form ) {
//product_cat is the taxonomy for WooCommerce's products
//get the terms for the product_cat taxonomy
$product_categories = get_terms( 'product_cat', array('hide_empty' => false,) );
//Creating drop down item array.
$items = array();
//Adding product category terms to the items array
foreach ( $product_categories as $product_category ) {
$items[] = array( 'value' => $product_category->name, 'text' => $product_category->name );
}
//Adding items to field id 6. Replace 6 with your actual field id. You can get the field id by looking at the input name in the markup.
foreach ( $form['fields'] as &$field ) {
if ( $field->id == 64 ) {
$field->choices = $items;
}
}
return $form;
}
我想我需要在这一行中添加类别的级别:
$items[] = array( 'value' => $product_category->name, 'text' => $product_category->name );
但是我该怎么做呢?
对我来说,如果每个级别在名称前都有一个 -
(第三级两个 -
...)就足够了。
像这样:
- Main category
-- Sub category
--- Third level category
我找到了解决办法。我已经像这样更改了 foreach
代码:
foreach ( $product_categories as $product_category ) {
$product_category_level = count( get_ancestors($product_category->term_id, 'product_cat'));
if ($product_category_level == 1 ) :
$product_category_level_indicator = '- ';
elseif ($product_category_level == 2 ) :
$product_category_level_indicator = '-- ';
else:
$product_category_level_indicator = '';
endif;
$items[] = array( 'value' => $product_category->name, 'text' => $product_category_level_indicator.$product_category->name );
}
对我有用。 感谢反馈。