向所有小部件添加选择框

时间:2012-04-17 作者:oblik

我想在我的站点侧栏中的所有小部件中添加一个默认的选择框。这将允许选择关联小部件的颜色。你知道我怎么做吗?我知道如何制作自定义小部件。非常感谢。

1 个回复
最合适的回答,由SO网友:oblik 整理而成

感谢Ed Nailor&;库克鲁特!

http://ednailor.com/2011/01/24/adding-custom-css-classes-to-sidebar-widgets/


    // __________________________________________________________
//
// CUSTOM CLASS BY WIDGETS

function kc_widget_form_extend( $instance, $widget ) {
 if ( !isset($instance[\'classes\']) )
 $instance[\'classes\'] = null;
 /* Set your predetermied class choices here */
 $myarray = "class1,class2,class3";

$myclasses = explode(",",$myarray);
 $row = "

\\n"; $row .= "\\tid_base}-{$widget->number}-classes\'>Class:\\n"; $row .= "\\tid_base}[{$widget->number}][classes]\' id=\'widget-{$widget->id_base}-{$widget->number}-classes\' class=\'widefat\'>\\n"; foreach($myclasses as $myclass) { $instance_selected = null; if($instance[\'classes\']==$myclass) $instance_selected = " selected=\'selected\'"; $row .= "\\t".$myclass."\\n"; } $row .= "\\n"; echo $row; return $instance; } add_filter(\'widget_form_callback\', \'kc_widget_form_extend\', 10, 2);function kc_widget_update( $instance, $new_instance ) { $instance[\'classes\'] = $new_instance[\'classes\']; return $instance; } add_filter( \'widget_update_callback\', \'kc_widget_update\', 10, 2 ); function kc_dynamic_sidebar_params( $params ) { global $wp_registered_widgets; $widget_id = $params[0][\'widget_id\']; $widget_obj = $wp_registered_widgets[$widget_id]; $widget_opt = get_option($widget_obj[\'callback\'][0]->option_name); $widget_num = $widget_obj[\'params\'][0][\'number\']; if ( isset($widget_opt[$widget_num][\'classes\']) && !empty($widget_opt[$widget_num][\'classes\']) ) $params[0][\'before_widget\'] = preg_replace( \'/class="/\', "class=\\"{$widget_opt[$widget_num][\'classes\']} ", $params[0][\'before_widget\'], 1 ); return $params; } add_filter( \'dynamic_sidebar_params\', \'kc_dynamic_sidebar_params\' );

结束