我正在制作一个非常基本的小部件,我在过去做了很多没有问题,但由于某些原因我的设置的某些区域将无法保存。即“$postTypeSelect
”,“$postTypeNumber
”,“$selectedPostTag
”的设置。这是在运行时“生成”的唯一项目,它们可能是问题,不确定。
<?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
}
}
?>
答案 0 :(得分:0)
问题是您的表单元素没有正确的name
属性(和id
):
你应该使用:
<select name="<?php echo $this->get_field_name( 'postTypeSelect' ); ?>" id="<?php echo $this->get_field_id( 'postTypeSelect' ); ?>"> .... </select>
<select name="<?php echo $this->get_field_name( 'postTypeNumber' ); ?>" id="<?php echo $this->get_field_id( 'postTypeNumber' ); ?>"> ... </select>
<input type="checkbox" name="<?php echo $this->get_field_name( 'selectedPostTag' ); ?>[]" />
<强>更新强>
此外,我发现$instance['selectedPostTag']
未正确更新,因为它可能包含多个值。
以下是您的小部件的update
和form
函数:
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'] = $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>
<select name="<?php echo $this->get_field_name( 'postTypeSelect' ); ?>">
<?php
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>';
?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'postTypeNumber' ); ?>"><?php _e('Number of Posts'); ?></label>
<select name="<?php echo $this->get_field_name( 'postTypeNumber' ); ?>">
<?php
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>';
}
?>
</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){
?>
<input type="checkbox" name="<?php echo $this->get_field_name( 'selectedPostTag' ) ?>[]" value="<?php echo $postTag->term_id ?>" <?php foreach ( $selected_tags as $checked ) { checked( $checked, $postTag->term_id, true ); }?> />
<?php echo $postTag->name ?>
</br>
<?php
}
?>
</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
}