Extra Argument in before_widget to determine its position in the sidebar

  1. fr33think3r

    It would be helpful to have a extra argument in before_widget

    $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_, $widgetnth);

    where $widgetnth is the position of the widget in the sidebar, i.e. whether its the first widget, second widget etc. This can be used in generating class definitions using which a theme developer can write widget independent, position dependent css.

    For Eg.
    'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s widget%3$s"><div class="widget-inside">'

    here widget%3s will generate class names like widget1, widget2 etc.

    This can be useful in various scenarios. One use case is

    2 widgets are stacked horizontally, and I want to set the width of the left widget as x and that of the right widget as y. There's no way to accomplish that without using hardcoded widget classes in the css.

    Posted: 5 years ago #
  2. fr33think3r

    The code here looks gibberish. I have blogged about this here with code and more details.

    Posted: 5 years ago #
  3. Andy Macaulay-Brook


    I could do with something like this myself, although it's usually the list elements in nav menus I want to count.

    However my feeling is that we should really be using css3 to style the nth elements in lists and perhaps using one of the available js libraries to extend such capabilities to IE.

    You could also look at hooking into the dynamic_sidebar_params filter to add your own counts into the classes. Look at /wp-includes/widgets.php for the source code of the dynamic_sidebar() function which calls this hook.

    Posted: 5 years ago #
  4. Here is the code I suggest:

    'before_widget' => '<div id="%1$s" class="widget %1$s widget-%2$s widget%3$s"><div class="widgetinside">'

    Posted: 11 months ago #
  5. matratzentest

    @healtwordpress: This Code doesnt work for me, which WP version are you using?

    Posted: 11 months ago #

RSS feed for this topic


You must log in to post.

  • Rating

    11 Votes
  • Status

    This idea is under consideration