我有marketplace插件,当卖家想先添加产品时,他会选择我想更改的类别,例如,我有2个主类别(A-B),我有(C-D-E)在A下面,我有(F-G-H)在B下面,我的代码
<td>
<label for="mp_seller_product_categories">Produkt kategorier</label>
</td>
<td>
<?php
$allowed_cat = get_user_meta(get_current_user_id(), \'wkmp_seller_allowed_categories\', true);
if (! $allowed_cat) {
$allowed_categories = get_option(\'wkmp_seller_allowed_categories\');
} else {
$allowed_categories = $allowed_cat;
}
require \'class-taxonomy-filter.php\';
$product_categories = wp_dropdown_categories(array(
\'show_option_none\' => __( \'\', \'marketplace\' ),
\'hierarchical\' => 1,
\'hide_empty\' => 0,
\'name\' => \'product_cate[]\',
\'id\' => \'mp_seller_product_categories\',
\'taxonomy\' => \'product_cat\',
\'title_li\' => \'\',
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'class\' => \'\',
\'exclude\' => \'\',
\'selected\' => array(),
\'echo\' => 0,
\'value_field\' => \'slug\',
\'walker\' => new MpProductCategoryTree($allowed_categories)
) );
echo str_replace( \'<select\', \'<select style="width: 50%;" data-placeholder="\'.__( \'Choose category(s)\', \'marketplace\' ).\'" multiple="multiple" \', $product_categories );
?>
</td>
我想换成
第一卖家将选择A或Bif如果选择A他可以选择(C或D或E),否则他选择Bhe可以选择(F或G或H)
我不想让代码从wordpress系统中获取类别,我想用代码编写类别
SO网友:Antti Koskinen
如果我正确理解了你的问题,那么像这样的事情可能会奏效。
// html markup for the selects
<select name="main_cat">
<option value="A">A</option>
<option value="B">B</option>
</select>
<select name="sub_cat_for_A" class="sub-cat-select hidden">
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
</select>
<select name="sub_cat_for_B" class="sub-cat-select hidden">
<option value="F">F</option>
<option value="G">G</option>
<option value="H">H</option>
</select>
如果您改变主意,想使用WordPress中的分类法,可以将标记更改为:,
<?php
$main_categories = array();
$sub_categories = array();
$cats = get_terms(array(
\'taxonomy\' => \'your_taxonomy\',
));
if ( $cats && ! is_wp_error( $cats ) ) {
foreach ( $cats as $cat ) {
if ( $cat->parent ) {
$sub_categories[$cat->parent][$cat->term_id] = $cat->name;
} else {
$main_categories[$cat->term_id] = $cat->name;
}
}
}
?>
<select name="main_cat">
<?php foreach ( $main_categories as $id => $name ) : ?>
<option value="<?php echo $id; ?>"><?php echo $name; ?></option>
<?php endforeach; ?>
</select>
<?php foreach ( $sub_categories as $main_cat_id => $sub_category_options ) : ?>
<select name="<?php echo "sub_cat_for_{$main_cat_id}" ?>" class="sub-cat-select hidden">
<?php foreach ( $sub_category_options as $id => $name ) : ?>
<option value="<?php echo $id; ?>"><?php echo $name; ?></option>
<?php endforeach; ?>
</select>
<?php endforeach; ?>
然后,您只需要一些jQuery(也可以使用普通javascript)来观察主选择上的更改并显示相关的子选择。
jQuery(document).on(\'ready\',function($){
$(\'select[name="main_cat"]\').on(\'change\',function(event){
var selectedMainCatId = $(this).value();
// hide other sub cat selects
$(\'.sub-cat-select\').addClass(\'hidden\');
// show correct sub cat select
$(\'select[name="sub_cat_for_\'+selectedMainCatId+\'"]\').removeClass(\'hidden\');
});
});
jQuery示例可能有改进的余地,但我相信您会从中了解到基本原理。