下面由WordPress教學專欄跟大家介紹怎麼為WordPress小工具加入CSS類別選項,希望對需要的朋友有幫助!
WordPress 小工具可以重複使用,相同的小工具樣式也是相同的,如果想自訂某個前台添加的小工具樣式,與其它不同,可以透過查看原始程式碼,找到它的id,例如搜尋小工具會看到類似的:
<section id="search-2" class="widget widget_search">
其中search-2就是這個小工具的ID,可以用#search-2定義樣式,不過這個後綴的編號並不是固定的,下次加可能會改變,需要再次編輯之前的樣式,有些麻煩。
可以透過下面的程式碼,為現有的小工具統一增加CSS類別選項,將程式碼加入目前主題函數模板functions.php即可,效果如圖:
程式碼一
只加入一個CSS類別選項
function zm_widget_form_extend( $instance, $widget ) { if ( !isset($instance['classes']) ) $instance['classes'] = null; $row = "<p>\n"; $row .= "\t<label for='widget-{$widget->id_base}-{$widget->number}-classes'>CSS类</label>\n"; $row .= "\t<input type='text' name='widget-{$widget->id_base}[{$widget->number}][classes]' id='widget-{$widget->id_base}-{$widget->number}-classes' class='widefat' value='{$instance['classes']}'/>\n"; $row .= "</p>\n"; echo $row; return $instance; } add_filter('widget_form_callback', 'zm_widget_form_extend', 10, 2); function zm_widget_update( $instance, $new_instance ) { $instance['classes'] = $new_instance['classes']; return $instance; } add_filter( 'widget_update_callback', 'zm_widget_update', 10, 2 ); function zm_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', 'zm_dynamic_sidebar_params' );
程式碼二
新增ID及CSS類別下拉選項
function zm_widget_form_extend( $instance, $widget ) { if ( !isset( $instance['classes'] ) ) $instance['classes'] = null; if ( !isset( $instance['custom_id'] ) ) $instance['custom_id'] = null; $class_prefix = 'widget-'; $myclass = array( 'default' => '默认', 'blue' => '蓝色', 'yellow' => '黄色', 'black' => '黑色', ); $row = "<p>\n"; $row .= "\t<label for='widget-{$widget->id_base}-{$widget->number}-custom_id'>添加ID</label>\n"; $row .= "\t<input type='text' name='widget-{$widget->id_base}[{$widget->number}][custom_id]' id='widget-{$widget->id_base}-{$widget->number}-custom_id' class='widefat' value='{$instance['custom_id']}' />\n"; $row .= "\t<label for='widget-{$widget->id_base}-{$widget->number}-classes'>CSS类</label>\n"; $row .= "\t<select name='widget-{$widget->id_base}[{$widget->number}][classes]' id='widget-{$widget->id_base}-{$widget->number}-classes' class='widefat'>"; foreach( $myclass as $key => $class ) { $selected = null; if( $class_prefix.$key == $instance['classes'] ) $selected = 'selected = "selected"'; $row .= "\t<option value='$class_prefix$key' $selected>$class</value>\n"; } $row .= "</select>\n"; echo $row; return $instance; } add_filter('widget_form_callback', 'zm_widget_form_extend', 10, 2); function zm_widget_update( $instance, $new_instance ) { $instance['classes'] = $new_instance['classes']; $instance['custom_id'] = $new_instance['custom_id']; return $instance; } add_filter( 'widget_update_callback', 'zm_widget_update', 10, 2 ); function zm_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 ); if ( isset($widget_opt[$widget_num]['custom_id']) && !empty($widget_opt[$widget_num]['custom_id']) ) $params[0]['before_widget'] = preg_replace( '/id=".*?"/', "id=\"{$widget_opt[$widget_num]['custom_id']}\"", $params[0]['before_widget'], 1 ); return $params; } add_filter( 'dynamic_sidebar_params', 'zm_dynamic_sidebar_params' );
程式碼中預設了CSS類別名稱,可能使用更方便。
缺點:新增的選項位置在其它小工具選項的上面,需要改進一下。
以上是怎麼為WordPress小工具加入CSS類別選項的詳細內容。更多資訊請關注PHP中文網其他相關文章!