2017-08-21 61 views
0

如何保存當前的「就業狀態」(表單功能)並將其顯示在前端(Widget功能)中?未保存的單選按鈕

// Create the widget output. 
public function widget($args, $instance) { 
    $title = apply_filters('widget_title', $instance[ 'title' ]); 
    $employment = apply_filters('widget_title', $instance[ 'employment' ]); 
    echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; ?> 
    <p>Current state of employment: <?php echo $employment ?></p> 
    <?php echo $args['after_widget']; 
} 


// Create the admin area widget settings form. 
public function form($instance) { 
    $title = ! empty($instance['title']) ? $instance['title'] : ''; 
    $employment = ! empty($instance['employment']) ? $instance['employment'] : 'help'; 
    ?> 
    <p> 
    <label> Current State of employment:</label><br> 
    <input for="<?php echo $this->get_field_id('employment'); ?>" type="radio" name="employment" value="employed"> Employed<br> 
    <input for="<?php echo $this->get_field_id('employment'); ?>" type="radio" name="employment" value="unemployed"> Unemployed<br> 
</p> 
<?php 
} 
+1

檢查這個https://stackoverflow.com/questions/45752478/wordpress-widget-dev-undefined-index-and-other-problems/45753392?noredirect= 1個#comment78464627_45753392 – vel

回答

1

試試這個代碼

<input type="radio" <?php checked($instance[ 'employment' ] ,'employment'); ?> id="<?php echo $this->get_field_id('employment'); ?>" value="employment" name="<?php echo $this->get_field_name('employment'); ?>" ><br> 

<input type="radio" <?php checked($instance[ 'employment' ] ,'unemployed'); ?> id="<?php echo $this->get_field_id('unemployed'); ?>" value="unemployed" name="<?php echo $this->get_field_name('employment'); ?>" >