更新函数时小部件复选框数组为空

时间:2014-03-29 作者:erichmond

我有一个小部件没有将复选框状态传递给$new\\u instance,特别是$instance[\'posts\'];如果我在更新函数中转储$new\\u实例,它是否为空?所有其他实例变量都正常,是否如预期的那样?

  /**
     * Ouputs the options form on admin
     *
     * @param array $instance The widget options
     */
    public function form( $instance ) {

        if ( isset( $instance[ \'type\' ] ) ) {
            $type = $instance[ \'type\' ];
        }
        else {
            $type = __( \'Select Type\', \'text_domain\' );
        }

        if ( isset( $instance[ \'icon\' ] ) ) {
            $icon = $instance[ \'icon\' ];
        } else {
            $icon = __( \'Select Icon\', \'text_domain\' );
        }

        if ( isset( $instance[ \'title\' ] ) ) {
            $title = $instance[ \'title\' ];
        } else {
            $title = __( \'New title\', \'text_domain\' );
        }

        if ( isset( $instance[ \'posts\' ] ) ) {
            $theposts[\'posts\'] = $instance[ \'posts\' ];
        }

        ?>
        <p>
        <label for="<?php echo $this->get_field_id( \'type\' ); ?>"><?php _e( \'Type:\' ); ?></label> 
        <select id="<?php echo $this->get_field_id(\'type\'); ?>" name="<?php echo $this->get_field_name(\'type\'); ?>" class="widefat" style="width:100%;">
            <?php
           $types = array(\'Select Type\', \'rental\' => \'Rental\', \'cloud\' => \'Cloud\', \'konnect\' => \'Connect\', \'support\' => \'Support\');
            foreach($types as $type => $v) { ?>

            <?php if ( isset( $instance[ \'type\' ] ) ) { ?>
                <option <?php if ( $instance[\'type\'] == $type ) echo \'selected="selected"\'; ?> value="<?php echo $type; ?>"><?php echo $v; ?></option>
            <?php } else { ?>
                <option  value="<?php echo $type; ?>"><?php echo $v; ?></option>
            <?php } ?>

        <?php } ?>      
        </select>
        </p>

        <p>
        <label for="<?php echo $this->get_field_id( \'icon\' ); ?>"><?php _e( \'icon:\' ); ?></label> 
        <select id="<?php echo $this->get_field_id(\'icon\'); ?>" name="<?php echo $this->get_field_name(\'icon\'); ?>" class="widefat" style="width:100%;">
            <?php
        $icons = array(\'Select Icon\', \'monitor\', \'users\', \'switch\', \'cloud\', \'arrows-ccw\', \'user\', \'user-add\', \'link\', \'lock\', \'thumbs-up\', \'upload-cloud\',\'login\',\'database\',\'install\',\'lifebuoy\',\'signal\',\'network\');
            foreach($icons as $ico) { ?>

            <?php if ( isset( $instance[ \'icon\' ] ) ) { ?>
                <option <?php if ( $instance[\'icon\'] == $ico ) echo \'selected="selected"\'; ?> value="<?php echo $ico; ?>"><?php echo $ico; ?></option>
            <?php } else { ?>
                <option  value="<?php echo $ico; ?>"><?php echo $ico; ?></option>
            <?php } ?>

            <?php } ?>       
       </select>
        </p>


        <p>
        <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\' ); ?></label> 
        <input class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
        </p>


            <p>
            <label for="<?php echo $this->get_field_id( \'posts\' ); ?>"><?php _e( \'Post to show:\' ); ?></label><br />

            <?php
            if ($instance [\'type\'] == \'konnect\') {$instance [\'type\'] == \'konect\';}
            $newargs = array(\'posts_per_page\' => -1, \'post_type\' => $instance[ \'type\' ], \'post_parent\' => 0,);
            $specificposts = get_posts( $newargs ); ?>

            <?php foreach($specificposts as $name => $specificpost) {  ?>


                <?php if ( in_array( $specificpost->ID, $instance[ \'posts\' ] ) ) { ?>
                    <p class="<?php echo $specificpost->ID; ?>"><input type="checkbox" name="<?php echo $instance[ \'posts\' ][$specificpost->ID]; ?>" value="<?php echo $specificpost->ID; ?>" checked="checked" /> <label><?php echo $specificpost->post_title; ?></label></p>
                <?php  } else { ?>
                    <p class="<?php echo $specificpost->ID; ?>"><input type="checkbox" name="<?php echo $this->get_field_name(\'posts\'). \'[\'.$specificpost->ID.\']\'; ?>" value="<?php echo $specificpost->ID; ?>" /> <label><?php echo $specificpost->post_title; ?></label></p>
                <?php }  ?>
          <?php } ?>

        </p>            

    <?php }

1 个回复
最合适的回答,由SO网友:magicroundabout 整理而成

我怀疑这主要是因为向下看,您的输入没有使用$this->get\\u field\\u name()(两个复选框输入中的第一个)。

尝试使用$this->get\\u field\\u name(),就像您在该部分代码的“then”子句中所做的那样,应该可以。

我看不出还有什么错。

结束