我有一个产品搜索小部件,当您选择某些产品时,会显示一个隐藏字段。我想在我的小部件选项中设置一个自定义类别,然后用于显示隐藏字段。
当我将父category\\u id手动添加到代码中时,前端可以100%工作。现在我只需要在后端设置category选项。
这是我目前拥有的,但它不起作用,因为它没有存储所做的选择。我省略了搜索表单,因为它没有使用任何小部件选项。
class Equipment_Search extends WP_Widget {
function Equipment_Search() {
$widget_ops = array( \'classname\' => \'agriquip\', \'description\' => __(\'Displays the Equipment Search Form\', \'agriquip\') );
$control_ops = array( \'width\' => 200, \'height\' => 350, \'id_base\' => \'agriquip-widget\' );
$this->WP_Widget( \'agriquip-widget\', __(\'Equipment Search\', \'agriquip\'), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters(\'widget_title\', $instance[\'title\'] );
$tract_id = isset( $instance[\'exc_equipment_cat\'] ) ? $instance[\'exc_equipment_cat\'] : false;
$tract = wp_list_pluck(get_terms(\'exc_equipment_cat\', array(\'parent\' => 3)), \'slug\');
$tractparent = get_term_by(\'id\',\'3\', \'exc_equipment_cat\');
$tractparent = $tractparent->slug;
echo $before_widget;
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
//Strip tags from title and name to remove HTML
$instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
$instance[\'exc_equipment_cat\'] = $new_instance[\'exc_equipment_cat\'] ? 1 : 0;
return $instance;
}
function form( $instance ) {
//Set up some default widget settings.
$defaults = array( \'title\' => __(\'Equipment Search\', \'agriquip\'), \'exc_equipment_cat\' => \'-1\');
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<p>
<label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e(\'Title:\', \'agriquip\'); ?></label>
<input id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo $instance[\'title\']; ?>" class="widefat" />
</p>
<form method="post" action="">
<label for="exc_equipment_cat">Category to use with Kw Options</label>
<?php
$dropdown_args = array(
\'taxonomy\' => \'exc_equipment_cat\',
\'name\' => $this->get_field_name(\'exc_equipment_cat\'),
\'show_count\' => 1,
\'orderby\' => \'name\',
\'hierarchical\' => true,
\'echo\' => 0,
\'depth\' => 1,
\'show_option_none\' => \'Select Category\',
\'selected\' => (int)$instance[\'exc_equipment_cat\'],
);
wp_dropdown_categories(apply_filters(\'widget_categories_dropdown_args\', $dropdown_args));
$select = wp_dropdown_categories(apply_filters(\'widget_categories_dropdown_args\', $dropdown_args));
$select = preg_replace("#<select([^>]*)>#", "<select$1 class=\'widefat\'>", $select);
echo $select;
?>
</form>
<?php
}