WP 4.5 widget refresh and adding classes dinamically
-
So, you have a grid, 4 columns, and each column represents a widget. Normally the last column has a class “.last-col” which removes margin-right. I am using this code to add the class to the last column.
$css_class = array(); $css_class[] = 'grid-col'; $css_class[] = 'grid-1x-col'; if( $widget_position % 4 == 0 ) { $css_class[] = 'last-col'; }; $css_classes = join(' ', $css_class); if ( ! empty( $css_classes ) ) { if( strpos($args['before_widget'], 'class') === false ) { $args[ 'before_widget' ] = str_replace( '>', 'class="'. esc_attr( $css_classes ) . '"', $args[ 'before_widget' ] ); } else { $args[ 'before_widget' ] = str_replace( 'class="', 'class="'. esc_attr( $css_classes ) . ' ', $args[ 'before_widget' ] ); } }
If you are in the Customizer and you add your widgets/columns, the last column class isn’t added in the preview window (with the latest selective refresh for widgets, I assume it has something to do with being a JS template). The code works perfectly without selective refresh, is there anyway around this?
I know I can do this with css nth-child, but just as an example…
- The topic ‘WP 4.5 widget refresh and adding classes dinamically’ is closed to new replies.