0
我正在製作一個非常基本的小部件,過去我做了很多沒有問題但由於某些原因我的設置的某些區域不能保存。即設置爲「$postTypeSelect
」,「$postTypeNumber
」,「$selectedPostTag
」。這是唯一在運行時「生成」的項目,它們可能是問題,不確定。爲什麼我的wordpress小部件設置不會保存?
<?php
add_action('widgets_init','register_postTypeWidget');
function register_postTypeWidget(){
register_widget('postType_widget_start');
}
class postType_widget_start extends WP_Widget {
function postType_widget_start() {
$widget_ops = array('classname' => 'postTypeWidget', 'description' => __('Display Posts by Post Type'));
$control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'posttype-widget');
$this->WP_Widget('posttype-widget', __('Posts by Post Type', 'postType_widget'), $widget_ops, $control_ops);
}
function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', $instance['title']);
$postTypeSelect = $instance['postTypeSelect'];
$postTypeNumber = $instance['postTypeNumber'];
$selectedPostTag = $instance['selectedPostTag'];
$showDate = $instance['showDate'];
$showAuthor = $instance['showAuthor'];
$args = array(
'posts_per_page' => $postTypeNumber,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => $postTypeSelect);
$postList = get_posts($args);
echo $before_widget;
?>
<!--
<p class="widget-title">News & Announcements</p>
<ul class="posts-by-tag-list">
<li class="posts-by-tag-item news" id="posts-by-tag-item-33">
<a class="posts-by-tag-item-title" href="http://dev.douglascountysoccer.com/intrinsicly-evolve-excellent-alignments/">Intrinsicly evolve excellent alignments</a> <small>Posted on: September 14, 2013</small>
</li>
</ul>
-->
<?php
echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['postTypeSelect'] = $new_instance['postTypeSelect'] ;
$instance['postTypeNumber'] = strip_tags($new_instance['postTypeNumber']);
$instance['selectedPostTag'] = strip_tags($new_instance['selectedPostTag']);
$instance['showDate'] = strip_tags($new_instance['showDate']);
$instance['showAuthor'] = strip_tags($new_instance['showAuthor']);
return $instance;
}
function form($instance) {
$defaults = array('title' => __(''), 'postTypeSelect' => __(''), 'postTypeNumber' => __(''), 'selectedPostTag' => __(''), 'showDate' => __(''), 'showAuthor' => __(''));
$instance = wp_parse_args((array) $instance, $defaults);
$args = '';
$output = 'object';
$post_types = get_post_types($args, $output);
$posttags = get_tags();
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title'); ?></label>
<input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id('postTypeSelect'); ?>"><?php _e('Select Post Type'); ?></label>
<?php
echo '<select name="postTypeSelect" style="width:100%;">';
echo '<option value="">Select Post Type</option>';
foreach($post_types as $post_type) {
if($instance['postTypeSelect'] == $post_type->name)
echo '<option value="'.$post_type->name.'" selected >'.$post_type->label.'</option>';
else
echo '<option value="'.$post_type->name.'" >'.$post_type->label.'</option>';
}
echo '</select>';
?>
</p>
<p>
<label for="<?php echo $this->get_field_id('postTypeNumber'); ?>"><?php _e('Number of Posts'); ?></label>
<?php
echo '<select name="postTypeNumber" style="width:100%;">';
else
echo '<option value="0">test</option>';
for($x=1; $x < 11; $x++) {
if($instance['postTypeNumber'] == $x)
echo '<option value="'.$x.'" selected >'.$x.'</option>';
else
echo '<option value="'.$x.'" >'.$x.'</option>';
}
echo '</select>';
?>
</p>
<p>
<label for="<?php echo $this->get_field_id('selectedPostTag'); ?>" style="display:block;width:100%;"><?php _e('Show Only Posts with These Tags (optional)'); ?></label>
<?php
foreach($posttags as $postTag){
if($instance['selectedPostTag'] == $postTag->term_id)
echo '<input type="checkbox" name="selectedPostTag[]" value="'.$postTag->term_id.'" checked />'.$postTag->name.'</br>';
else
echo '<input type="checkbox" name="selectedPostTag[]" value="'.$postTag->term_id.'" />'.$postTag->name.'</br>';
}
?>
</p>
<p>
<label for="<?php echo $this->get_field_id('additional_options'); ?>" style="display:block;width:100%;"><?php _e('Additional Options'); ?></label>
<?php if($instance['showDate'] == 'showDate'): ?>
<input type="checkbox" id="<?php echo $this->get_field_id('showDate'); ?>" name="<?php echo $this->get_field_name('showDate'); ?>" value="showDate" checked />Show Date</br>
<?php else: ?>
<input type="checkbox" id="<?php echo $this->get_field_id('showDate'); ?>" name="<?php echo $this->get_field_name('showDate'); ?>" value="showDate" />Show Date</br>
<?php endif; ?>
<?php if($instance['showAuthor'] == 'showAuthor'): ?>
<input type="checkbox" id="<?php echo $this->get_field_id('showAuthor'); ?>" name="<?php echo $this->get_field_name('showAuthor'); ?>" value="showAuthor" checked />Show Author</br>
<?php else: ?>
<input type="checkbox" id="<?php echo $this->get_field_id('showAuthor'); ?>" name="<?php echo $this->get_field_name('showAuthor'); ?>" value="showAuthor" />Show Author</br>
<?php endif; ?>
</p>
<?php
}
}
?>