Making Smart Widgets in a Grid Layout

Making Smart Widgets in a Grid Layout

For those of you who like to use a Grid system in your themes, here is a neat trick I use. Instead of hard coding your widgets i.e. featured-a, featured-b, you could make one widget called ‘featured’ and have it determine the correct layout. You can use ‘wp_get_sidebars_widgets()’ to count how many widgets the user has activated in that spot to dynamically alter your grid width. If the user has 2 widgets activated in ‘featured’, then the grid width would change to 6 in a 12 column layout. If the user has 4 items activated, then it would change to a grid width of 3. Here is an example using Bootcamp’s scaffolding for the grid system.

Adding your widgets in an array will save you the effort of writing out each widget, we will load it in a foreach loop:

First, we calculate how many widgets are activated with ‘wp_get_sidebars_widgets()’. Then we called the function calc_span() and we pass along how many widgets are activated in the current widget, which will return the correct span width.

Depending on which grid system you use, you may need to change the return value. Now when a user adds and removes widgets, the layout will adjust accordingly.

Tags:
No Comments

Sorry, the comment form is closed at this time.