Wordpress 小部件中的复选框数组? [英] Checkbox array in Wordpress widget?

查看:25
本文介绍了Wordpress 小部件中的复选框数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个小部件,使用户能够选择将显示哪些类别.以下是我创建的一段代码,但无法保存复选框状态的更改.只有两个值:标题和所选类别的列表.

I'm trying to create a widget that enables user to select which categories will be displayed. The following is piece of codes I created but the changes of the checkbox state can't be saved. There are only two values : the title and list of selected categories.

function form($instance) {
    $instance = (array)$instance;
    if( empty($instance['title']) ) $instance['title'] = 'Category';
    $selected_categories = (array)$instance['category'];
    var_dump($selected_categories); 
    ....
    $categories = get_categories( array('exclude'=> 1) );
    foreach($categories as $category) : ?>
    <div>
        <input type="checkbox" name="<?php echo $this->get_field_name('category'); ?>"
            value="<?php echo $category->cat_ID; ?>"
            <?php echo $category->cat_name; ?>
    </div>
    <?php endforeach; ?>
}

function update($new_instance, $old_instance) {
    $instance = $old_instance;
    $instance['title'] = strip_tags($new_instance['title']);
    $instance['category'] = $new_instance['category'];

    return $instance;
}

<?php endforeach;?>}函数更新($new_instance,$old_instance){$instance = $old_instance;$instance['title'] = strip_tags($new_instance['title']);$instance['category'] = $new_instance['category'];返回 $instance;}

I observe the changes through var_dump($selected_categories). The value is always array(size=0) ignoring how many checkbox I checked.

我通过 var_dump($selected_categories) 观察变化.该值始终为 array(size=0),忽略我选中的复选框数量.

I have no idea how to passing array in $instance variable.

我不知道如何在 $instance 变量中传递数组.

Thanks in advance.

提前致谢.

推荐答案

这是使用复选框数组的第二种解决方案.我在我的小部件 最近更新的帖子小部件的新版本 1.8 中使用复选框数组解决了这个问题.我使用foreach的第二种形式

foreach (array_expression as $key => $value).

$key allows to obtain a unique id for each checkbox. 
Here $id is a number incremented by the foreach itself.

$key 允许为每个复选框获取唯一 ID.这里 $id 是一个由 foreach 自身递增的数字.

function form($instance) { $term_taxonomy_id = (isset($instance['term_taxonomy_id']) ? array_map('absint', $instance['term_taxonomy_id']) : array("0")); <?php $categ = get_categories(); foreach($categ as $id => $item) : ?> <br/> <input type="checkbox" id="<?php echo $this->get_field_id('term_taxonomy_id') . $id; ?>" name="<?php echo $this->get_field_name('term_taxonomy_id'); ?>[]" <?php if (isset($item->term_taxonomy_id)) { if (in_array($item->term_taxonomy_id,$term_taxonomy_id)) echo 'checked'; }; ?> value="<?php echo $item->term_taxonomy_id; ?>" /> <label for="<?php echo $this->get_field_id('term_taxonomy_id') . $id; ?>" > <?php echo $item->name ?> </label> <?php endforeach; ?>

I stored term_taxonomy_id instead cat_ID, for my plugin, but you can pretty much store cat_ID, it will work as well.

我为我的插件存储了 term_taxonomy_id 而不是 cat_ID,但是您几乎可以存储 cat_ID,它也可以正常工作.>

数组数据由php函数array_map()清理.更新函数为:

The array data is sanitized by the php function array_map ().The update function is:

 function update($new_instance, $old_instance) {
// Par défaut, aucune catégorie n'est exclue
    $instance['term_taxonomy_id'] =  (isset($new_instance['term_taxonomy_id']) ? array_map( 'absint', $new_instance['term_taxonomy_id']) : array('0'));

为了在 MySQL 查询中使用,我将其内爆为一组值:

For their use in a MySQL query, I implodes it into a set of values:

// écriture de la liste des objects sélectionnées en langage MySQL
    if (isset($instance['term_taxonomy_id'])) 
        $selected_object = "('".implode(array_map( 'absint',$instance['term_taxonomy_id']),"','")."')"; 
    else $selected_object = "('0')";

这篇关于Wordpress 小部件中的复选框数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆