多亏了这里的另一篇文章,我为所有WP小部件设置了一个额外的字段(称为“字幕”)。现在,我试图让所有WP小部件都显示这个额外字段的值(当然,在\\u title之后,因为它是副标题),但我不太明白这一点。
下面是我用来为所有小部件创建额外字段的代码:
function gcc_in_widget_form($t,$return,$instance){
$instance = wp_parse_args( (array) $instance, array( \'title\' => \'\', \'text\' => \'\', \'float\' => \'none\') );
if ( !isset($instance[\'subtitle\']) )
$instance[\'subtitle\'] = null;
?>
<!-- SUBTITLE -->
<p>
<label for="<?php echo $t->get_field_id( \'subtitle\' ); ?>"><?php _e( \'Subtitle:\' ); ?></label>
<input class="widefat" id="<?php echo $t->get_field_id( \'subtitle\' ); ?>" name="<?php echo $t->get_field_name( \'subtitle\' ); ?>" type="text" value="<?php echo $instance[\'subtitle\'];; ?>" />
</p>
$return = null;
return array($t,$return,$instance);
}
function gcc_in_widget_form_update($instance, $new_instance, $old_instance){
$instance[\'subtitle\'] = strip_tags($new_instance[\'subtitle\']);
return $instance;
}
add_action(\'in_widget_form\', \'gcc_in_widget_form\',5,3);
add_filter(\'widget_update_callback\', \'gcc_in_widget_form_update\',5,3);
add_filter(\'dynamic_sidebar_params\', \'gcc_dynamic_sidebar_params\');
下面是我如何在前端显示字段的值:
function gcc_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\'];
$params[0][\'after_title\'] .= \'how to display the value of the instance[\'subtitle\']??\';
return $params;
}
你能给我指一下正确的方向吗?
最合适的回答,由SO网友:s_ha_dum 整理而成
过滤器不会打开widget_title
这样做?
function widget_title_wpse_206841($title,$instance) {
// var_dump($instance);
if (!empty($instance[\'subtitle\'])) {
$title .= \' something \'.$instance[\'subtitle\'].\' something \';
}
return $title;
}
add_filter(\'widget_title\',\'widget_title_wpse_206841\',10,2);