我正在嘗試爲woocommerce產品創建自己的wordpress小部件插件。所以我有一些編碼,但堅持存儲檢查/選擇的類別的問題。如何在wordpress小部件中創建多個複選框
public function form($instance) {
$instance = wp_parse_args((array) $instance, array('title' => '', 'select_product_categories' => ''));
$args = array('orderby' => 'name', 'order' => 'ASC', 'hide_empty' => true);
$show_pcategories = get_terms('product_cat', $args);
$title = strip_tags($instance['title']);
$select_product_categories = $instance['select_product_categories'];
<?php /* Select Categories Checkbox to display */ ?>
<p><label for="<?php echo $this->get_field_id('select_product_categories'); ?>">Select Product Category to display:</label><br>
<?php
foreach($show_pcategories as $category) {
?>
<label for="<?php echo $this->get_field_id('select_product_categories') . $category->term_id; ?>"><input id="<?php echo $this->get_field_id('select_product_categories') . $category->term_id; ?>" name="<?php echo $this->get_field_name('select_product_categories'); ?>[]" type="checkbox" value="<?php echo $category->name; ?>" <?php checked('1', $category->name); ?> /><?php echo $category->name; ?></label><br>
<?php
}
}
其提取woocommerce的所有產品類別,但是當我檢查了一些類別和保存......它不工作:(
好心幫我傢伙
======== =============================
SOLUTION
我只是想和它現在的工作很好!
foreach($show_pcategories as $category) {
?>
<label><input id="<?php echo $this->get_field_id('select_product_categories') . $category->term_id; ?>" name="<?php echo $this->get_field_name('select_product_categories'); ?>[]" type="checkbox" value="<?php echo $category->term_id; ?>" <?php foreach ($select_product_categories as $checked) { checked($checked, $category->term_id, true); } ?>><?php echo $category->name; ?></label><br>
<?php
}
我用foreach循環用於驗證...,還可以定義變量的陣列
$ select_product_categories =(數組)$實例[ 'select_product_categories'];
你如何保存數據庫中的值?如果您無法確定複選框是否已被選中,您有兩種可能的解決方案:1 /'$ _POST ['x']'被設置,當且僅當複選框「x」已被選中。 2 /爲每個名爲'x'的複選框添加一個'hidden'字段,該字段也被命名爲'x',其默認值爲。如果複選框'x'沒有被選中,那麼你將在$ _POST ['x']'中得到默認值。 – Martin
抱歉說!但我不明白! –