Add the Order of Widgets Within a Sidebar as a CSS Class to All Widgets


/ Published in: PHP
Save to your folder(s)

Here is a simple filter to automatically add a class attribute like widget-order-1 to all widgets within sidebars (from konstruktors.com)


Copy this code and paste it in your HTML
  1. add_action('init', 'add_widget_order_class');
  2. function add_widget_order_class() {
  3. global $wp_registered_sidebars, $wp_registered_widgets;
  4. $sidebars = wp_get_sidebars_widgets();
  5. if (empty($sidebars))
  6. return;
  7. foreach ($sidebars as $sidebar_id => $widgets) {
  8. if (empty($widgets))
  9. continue;
  10. foreach ($widgets as $i => $widget_id) {
  11. $order = $i + 1;
  12. $wp_registered_widgets[$widget_id]['classname'] .= ' widget-order-' . $order;
  13. }
  14. }
  15. }

URL: http://konstruktors.com/blog/wordpress/3615-add-widget-order-css-class-sidebar/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.