WordPress.org

Ready to get started?Download WordPress

Forums

Widgets and alternating styles (4 posts)

  1. patsofficecom
    Member
    Posted 8 years ago #

    Hello,

    I'm trying to "widgetize" my current theme and I'm running into a bit of a problem. What I'd like to do is have alternating styles applied to each widget in the sidebar. In my case, this would be alternating colors. In my old static sidebar, I simply had a counter that I took the remainder of when dividing by two. I can't do that with widgets.

    Does anybody know of a theme that does this already so I can take a look or know of a way to accomplish that.

    Thanks,

    Pat

  2. patsofficecom
    Member
    Posted 8 years ago #

    Bump...

  3. Samuel Wood (Otto)
    Tech Ninja
    Posted 8 years ago #

    That is a tricky one, isn't it?

    You could do it by modifying the widgets.php file itself. You'll want to modify the dynamic_sidebar function.

    In there is a foreach loop. It contains a sprintf function call. This sprintf is creating the list item for each widget. The last parameter of this sprintf is the contents of the class for that li.

    What you want to do is to add a counter or something like that in here. When the loop counter is odd, you append " widgetodd" to that last parameter of the sprintf, when it's even, you append " widgeteven" to it. This will add alternating classes for your widgets going down the page, and then you can style the odds and evens separately in your CSS file.

    Interesting notion, I like it.

  4. patsofficecom
    Member
    Posted 8 years ago #

    Thanks! I'll give it a look.

    Regards,

    Pat

Topic Closed

This topic has been closed to new replies.

About this Topic