编辑:忘记-将此代码添加到函数中。php
function nb_widget_number( $params ) {
$widget_id = $params[0][\'id\'];
$widget_array = wp_get_sidebars_widgets();
$widget_count = count( $widget_array[$widget_id] );
// set widget number to array if not set already
if( !$nb_widget_num )
$nb_widget_num = array();
// if no widgets, return params
if( !isset( $widget_array[$widget_id] ) || !is_array( $widget_array[$widget_id]) )
return $params;
// if widget number set, add 1, if not, set to 1
isset( $nb_widget_num[$widget_id] ) ? $nb_widget_num[$widget_id] ++ : $nb_widget_num[$widget_id] = 1;
// now you can do anything with that count you want. in this case, place something after the nth widget (1st for this example)
if( $nb_widget_num[$widget_id] == 1 )
$params[0][\'after_widget\'] = \'SOME CODE\';
return $params;
}
然后您只需添加过滤器,让wordpress知道如何使用此功能:
add_filter( \'dynamic_sidebar_params\', \'nb_widget_number\');
除了在小部件之后添加代码之外,您还可以使用它来添加代码、修改类(例如first/last),或者真正更改小部件$params的任何特性。