php 小部件多选小部件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 小部件多选小部件相关的知识,希望对你有一定的参考价值。

<?php

// Custom Text Widget without <div>
class Widget_Products extends WP_Widget {

	function Widget_Products() {
		$widget_ops = array('classname' => 'widget_products', 'description' => __('Latest products'));
		$this->WP_Widget('products', __('Products'), $widget_ops);
	}

	function widget( $args, $instance ) {
		global $post;
		extract($args);
		$title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
		$ingredientstitle = apply_filters( 'widget_title', empty($instance['ingredientstitle']) ? '' : $instance['ingredientstitle'], $instance, $this->id_base);
		$products = empty($instance['products']) ? '' : $instance['products'];
		
		echo $before_widget;
		
		if($_GET['shopby'] == 'ingredients'):
			
			if ( !empty( $ingredientstitle ) ) { echo $before_title . $ingredientstitle . $after_title; }
			
			$product_tags = get_terms(
					'product_category', array(
						'hide_empty' => 0,
						'orderby' => 'order',
						'parent' => INGREDIENTS
					)
				);
	
			if($product_tags):
			?>
				
				<ul>
					<?php foreach($product_tags as $tag): ?>
						<li><a href="<?php echo get_term_link($tag); ?>"><?php echo $tag->name; ?></a></li>
					<?php endforeach; ?>
				</ul>
			<?php else: ?>
				<h4>Terms not found</h4>
			<?php
			endif;
			
	else:
		if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }
			if(is_array($products)):
				$args = array( 'post_type' => 'product', 'post__in' => $products, 'post__not_in' => array($post->ID), 'orderby' => 'menu_order', 'order' => 'ASC');
				$r = new WP_Query($args);
				if($r->have_posts()):
				?>
					<ul>
						<?php while($r->have_posts()): $r->the_post(); ?>
							<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
						<?php endwhile; ?>
					</ul>
				<?php else: ?>
					<h4>Posts not found</h4>
				<?php
				endif;
			endif;
		wp_reset_query();
			
	endif;
	echo $after_widget;
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['ingredientstitle'] = strip_tags($new_instance['ingredientstitle']);
		$instance['products'] = $new_instance['products'];
		return $instance;
	}

	function form( $instance ) {
		$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
		$title = strip_tags($instance['title']);
		$ingredientstitle = strip_tags($instance['ingredientstitle']);
		$products = $instance['products'];
?>
		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('More Products 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>
		
		<?php query_posts(array('post_type' => 'product', 'post_status' => 'publish', 'showposts' => -1)); ?>
		<?php if(have_posts()) : ?>
			<p>
				<label for="<?php echo $this->get_field_id('products'); ?>"><?php _e('Products:'); ?></label>
				<select class="widefat" id="<?php echo $this->get_field_id('products'); ?>" name="<?php echo $this->get_field_name('products'); ?>[]" multiple="multiple">
					<?php while(have_posts()) : the_post(); ?>
						<option value="<?php echo get_the_ID(); ?>" <?php if(in_array(get_the_ID(), $products)): ?>selected="selected"<?php endif; ?>><?php the_title(); ?></option>
					<?php endwhile;  ?>
				</select>
			</p>	
		<?php endif; wp_reset_query(); ?>
		
		<p><label for="<?php echo $this->get_field_id('ingredientstitle'); ?>"><?php _e('More Ingredients Title:'); ?></label>
		<input class="widefat" id="<?php echo $this->get_field_id('ingredientstitle'); ?>" name="<?php echo $this->get_field_name('ingredientstitle'); ?>" type="text" value="<?php echo esc_attr($ingredientstitle); ?>" /></p>
<?php
	}
}

add_action('widgets_init', create_function('', 'return register_widget("Widget_Products");'));

以上是关于php 小部件多选小部件的主要内容,如果未能解决你的问题,请参考以下文章

禁用多选 Django 小部件中的选择

Kendo ui 多选验证

以自定义表单呈现模板中的各个字段

Flutter 滚动视图到列上的焦点小部件

使用多选通配符过滤数据源

Flutter:使用复选框进行下拉多选