Ferodynamics Network

popular: profile privacy, mobile privacy

November 13, 2007

A few weeks ago I showed how the home page contents could be moved into a widget to allow additional content to be added above and below it. That’s an interesting technique but not as interesting as this one. Using nested widgets to control the overall layout.

By nesting widgets it is a fairly simple process to allow the sidebar to switch from left to right, and to control the content in both the sidebar and the main content. I have created a sample theme that contains only the code necessary to do this, you can download it at the bottom of the page.

First you need to create the necessary widgets and register the sidebars, this is done in the functions.php file in your theme:

[php]

function sidebar_container_widget(){

?>

}

function content_container_widget(){

?>

}

function content_widget(){

?>

}

if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'layout',
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
));
register_sidebar(array(
'name' => 'sidebar',
'before_widget' => '

',
'after_widget' => '

',
'before_title' => '',
'after_title' => '',
));
register_sidebar(array(
'name' => 'main content',
'before_widget' => '

',
'after_widget' => '

',
'before_title' => '',
'after_title' => '',
));

register_sidebar_widget('Sidebar Widget',sidebar_container_widget);
register_sidebar_widget('Content Widget',content_container_widget);
register_sidebar_widget('Home Page Content',content_widget);

}

In the code above I have registered three sidebars, the home page layout, the main content, and the sidebar content. I have registered three widgets, one that includes the main content, one that includes the sidebar and one that contains the WordPress loop that would normally be on the home page. In the example I have included one more sidebar and widget, a full width banner than can be inserted above or below both the content and sidebar.

Next you need to set up your styles in an appropriate way to be sure that the sidebar and content can be interchangeable. I have floated them both left with an all round margin.

[css]
#content {
float:left;
width:500px;
border:1px solid #cccccc;
margin:10px;
padding:5px;
}

#sidebar {
float:left;
width:200px;
border:1px solid #cccccc;
margin:10px;
padding:5px;
}

Next you need to setup your index page. This is a simple prospect as there will not be any remaining content.

[php]

You should now have everything you need to make your layout work. In the example theme I have set up the widgets like this:

Widget Setup Diagram

Which results in the following layout:

Widget Diagram

To move the sidebar over to the left is a simple matter of swapping the sidebar and content widgets around.

You can download the sample theme here. It contains only three files, those necessary to illustrate the technique.

Post Image The Easy Peasy Way
26 comments
page 1065
Quick N Dirty Admin Login Screen
no comment
page 128
Wordpress Chat
one comment
page 1308
Html 5 Gallery
6 comments
page 1305
Html 5 Gallery
6 comments
page 1305
Silence Is Golden
3 comments
page 213
Questions About Habari For Wordpress Users
6 comments
page 424
Theming Habari Vs Wordpress
13 comments
page 440
My Experience Of Flexx
4 comments
page 1026
Plugin Update Fun With Photo Data 2
one comment
page 815
Post Image The Easy Peasy Way
26 comments
page 1065
Categories Vs Tags Either Neither Or Both
12 comments
page 7
Gaining Benefits From Plugins
8 comments
page 1167
Fun With Theme Widgets
24 comments
page 867
Beware Wp Cache
8 comments
page 1310
Six Million Ways To Die Choose One
14 comments
page 1128
Post Image The Easy Peasy Way
26 comments
page 1065
Post Image The Easy Peasy Way
26 comments
page 1065
Wordpress Chat
one comment
page 1308
Post Image The Easy Peasy Way
26 comments
page 1065
Wordpress Chat
one comment
page 1308
Beware Wp Cache
8 comments
page 1310
Wp Polls Reviewed
one comment
page 58
Fun With Photo Data
12 comments
page 330
Fun With Sidebar Tabs Styling
2 comments
page 336
Html 5 Gallery
6 comments
page 1305
Using Your Own Url Shortener
4 comments
page 1190
Html 5 Gallery
6 comments
page 1305
My Experience Of Flexx
4 comments
page 1026
Fun With Sidebar Tabs
193 comments
page 57
Html 5 Gallery
6 comments
page 1305
  updated 1 seconds ago
Friday, 8am
Widget Management Concepts – WP FUN

[...] the scenario described about, and which I wrote about in a previous post — changing layouts using widgets, many more places use widgets to provide options. For example, the main area could contain a single [...]

Tuesday, 9pm
Christy

This is a great post that really gets my head spinning with the potential! Definitely something I would have to draw out to keep straight lol. Let’s make it completely boggling and add in conditional tags ;). I hope this is a glimpse into Wordpress of the future.

Saturday, 12am
Ian Stewart

Andrew, this is very cool. Thank you for this.

Thursday, 6pm
arham | blogpreneur

just one word.. awsome :-)

Wednesday, 6am
Andrew Rickmann

It is a temptation isn’t it? I decided not to take it much further because actually just this little bit proved to be a little confusing at times.

That may be just a bad naming policy on my part though.

Tuesday, 10pm
Michael Wender

I really like the potential of this technique. I already imagine that I’ll be loosing sleep as I brainstorm about ways to take advantage of this.