无法解析小部件单选按钮

时间:2013-06-19 13:03:18

标签: php wordpress widget

我正在使用自定义小部件创建wordpress主题,所以我想制作一个带有3个单选按钮的示例小部件,到目前为止一直很好,但是update()函数在这里效果不好是我的代码:

function update( $new_instance, $old_instance ) {

$instance = $old_instance;

$instance['default-image'] = strip_tags($new_instance['default-image']);
$instance['post-image'] =  strip_tags($new_instance['post-image']);
$instance['link-image'] =  strip_tags($new_instance['link-image']);

return $instance;

}

function form( $instance ) {

$defaults = array('default-image' => true, 'post-image' => false, 'link-image' => false );
$instance = wp_parse_args( (array) $instance, $defaults ); 

?>

<p>
    <input name="option" type="radio" <?php checked($instance['default-image']); ?> id="<?php echo $this->get_field_id('default-image'); ?>"/> 
    <label for="<?php echo $this->get_field_id( 'default-image' ); ?>"><?php _e('Default image'); ?></label>
</p>
<p>
    <input name="option" type="radio" <?php checked( $instance['post-image']); ?> id="<?php echo $this->get_field_id('post-image'); ?>" /> 
    <label for="<?php echo $this->get_field_id( 'post-image' ); ?>"><?php _e('First post image'); ?></label>
</p>
<p>
    <input name="option" type="radio" <?php checked( $instance['link-image']); ?> id="<?php echo $this->get_field_id('link-image') ?>" /> 
    <label for="<?php echo $this->get_field_id( 'link-image' ); ?>"><?php _e('Image from link'); ?></label>
</p>

我将在widget()中使用$ instance ['default-image'],$ instance ['post-image'],$ instance ['link-image']提供它们,但是现在开始“保存”按钮或(在管理页面上刷新)所选选项消失。如果有人能告诉我我哪里错了,我会很感激。

2 个答案:

答案 0 :(得分:1)

回答这个问题,我在投资几个小时后发现,目前还没有明确的方法。 “radio”按钮上的Name属性应与foreach按钮相同,但要正常工作(保存并获取保存的值),名称应由wordpress通过echo给出($ this-&gt; get_field_name('default / post / link- image');我发现了另一种使用数值的解决方案: 更新函数():

function update( $new_instance, $old_instance ) {

    $instance = $old_instance;

    $instance['rating'] = ( isset( $new_instance['rating'] ) && $new_instance['rating'] > 0 && $new_instance['rating'] < 4 ) ? (int) $new_instance['rating'] : 0;

    return $instance;
}

和:

function form( $instance ) {

        $instance = wp_parse_args( (array)$instance, $defaults ); 
        //default value
        $rating = ( isset( $instance['rating'] ) && is_numeric( $instance['rating'] ) ) ? (int) $instance['rating'] : 1;
        ?>
            <p>
            <legend>Use image for:</legend>
                <input type="radio" id="<?php echo ($this->get_field_id( 'rating' ) . '-1') ?>" name="<?php echo ($this->get_field_name( 'rating' )) ?>" value="1" <?php checked( $rating == 1, true) ?>>
                <label for="<?php echo ($this->get_field_id( 'rating' ) . '-1' ) ?>"><?php _e('Default image') ?></label> <br />

                <input type="radio" id="<?php echo ($this->get_field_id( 'rating' ) . '-2') ?>" name="<?php echo ($this->get_field_name( 'rating' )) ?>" value="2" <?php checked( $rating == 2, true) ?>>
                <label for="<?php echo ($this->get_field_id( 'rating' ) . '-2' ) ?>"><?php _e('First image post') ?></label> <br />

                <input type="radio" id="<?php echo ($this->get_field_id( 'rating' ) . '-3') ?>" name="<?php echo ($this->get_field_name( 'rating' )) ?>" value="3" <?php checked( $rating == 3, true) ?>>
                <label for="<?php echo ($this->get_field_id( 'rating' ) . '-3' ) ?>"><?php _e('Image from internet. Note please save first') ?></label> <br />  
            </p>
    <?php
    }

答案 1 :(得分:0)

真的需要strip_tags功能吗?

function update($new, $old) {
    return $new;
}

我正在使用我的小部件^^