我试图创建窗口小部件,使用户能够选择哪些类别将被显示。 下面是一块,我创建的代码,但该复选框状态的改变无法保存。 只有两个值:标题和选择的类别列表。
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;
}
我观察过的var_dump的变化($ selected_categories)。 该值始终阵列(大小= 0)忽略我多少选中复选框。
我不知道如何传球达阵在$实例变量。
提前致谢。
你有没有,如果没有设置它们填充数组中的值的验证功能? 这是小工具的»更新«方法。 也许你应该加上:
'<input name="..." value="..." id="'.$this->get_field_id( 'category' ).'" />';
您输入的元素。
我花了很多时间在这个确切的问题。 在网上搜索关于这个主题均小于满意。 我想了解该Widget如何扩展WP_Widget对象的作品。 像原来的职位,我也想向我的允许管理员从所有职位类别的动态检查列表中进行选择。 在我的网站,我用这对宣传片,我希望有很多这条小狗的实例。 一个强大的实例存储系统是必不可少的我。
一对夫妇的那名缠着我愚蠢的问题。 1)这是怎么回事与时髦的标签= ....类别名称/标签的事情,我每例如在网上看到的。 (注“标签”是由左/右环绕符号..不能这样做,在这里..)是必需的,或者仅仅是某人抄袭什么人做过(在WordPress源代码的文件默认-widgets.php内) ? 答:时间完全是浪费。 少即是多,代码是正确的。
2)我真的想了解系统如何节省了用户的选择机制。 似乎有后台页面上没有明显的编码来捕捉选择。 我在这里度过了很长的时间来寻找,其中包括企图步骤直通代码的调试器,与糟糕的结果。 我花了很多时间的var_dump($实例),了解结果。 该系统的工作原理,但我真的不明白为什么。
我在做这个系统的工作逻辑是建立一个单一的复选框变量,得到那个工作。 下一步是创建复选框的数组变量,但只使用其中之一,并得到那个工作。 最后利用复选框变量的整个阵列。 成功。
代码片段如下:
/*Saves the settings. */
function update( $new_instance, $old_instance ){
$args = array(
//your selections here.
);
$categories = get_categories( $args ); // returns an array of category objects
$arrlength=count($categories);
for($x=0;$x<$arrlength;$x++){
$tempArray[$categories[$x]->slug] = '';
}
$instance = $old_instance;
$new_instance = wp_parse_args( (array) $new_instance, $tempArray );
for($x=0;$x<$arrlength;$x++){
$instance[$categories[$x]->slug] = $new_instance[$categories[$x]->slug] ? 1 : 0;
}
return $instance;
}
/*Creates the form for the widget in the admin back-end. */
function form( $instance ){
echo '<p>Choose your categories of interest. Multiple selections are fine.</p> ';
$args = array(
//your selections here. Use same args as update function, duh.
);
$categories = get_categories( $args ); // returns an array of category objects
$arrlength=count($categories);
for($x=0;$x<$arrlength;$x++){
$tempArray[$this->get_field_id($categories[$x]->slug)] = '';
}
$instance = wp_parse_args( (array) $instance, $tempArray );
for($x=0;$x<$arrlength;$x++){
$tempCheckFlag[$categories[$x]->slug] = $instance[$categories[$x]->slug] ? 'checked="checked"' : '';
// could also use 'checked()' function
// Yup, this could be combined with the for loop below,
// listed here seperately to enhance understanding of what's going on.
}
for($x=0;$x<$arrlength;$x++)
{
echo '<p><input class ="checkbox" type="checkbox" value="1" id="'.$this->get_field_id($categories[$x]->slug).'" name="'.$this->get_field_name($categories[$x]->slug).'"'.$tempCheckFlag[$categories[$x]->slug].'>'.$categories[$x]->name.' (Category # '.$categories[$x]->term_id.' )</p>';
}
}
当我需要使用的选择在我的窗口小部件的功能,我检索来自$实例变量管理员的选择。
/* Displays the Widget in the front-end */
function widget( $args, $instance ){
//....
foreach($instance as $key=>$value)
{
if ($value){
$category_array[]=$key;
}
}
// Now I have a simple array of just those categories that had a check mark...
下面是一个使用复选框的阵列的第二溶液。 我解决了这个问题,在我的窗口小部件的新版本1.8复选框阵列最近更新的帖子窗口小部件 。 我使用的foreach的第二种形式
foreach (array_expression as $key => $value).
$键可以获取每个复选框一个唯一的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; ?>
我存储term_taxonomy_id代替CAT_ID,我的插件,但你几乎可以储存CAT_ID,它也能发挥作用。
阵列数据由PHP函数array_map()。该更新功能是消毒:
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查询使用,我内爆成一组值:
// é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')";