888888.888888.88""Yb..dP"Yb..8888b..Yb..dP.88b.88....db....8b....d8.88..dP""b8..dP"Y8
88__...88__...88__dP.dP...Yb..8I..Yb.YbdP..88Yb88...dPYb...88b..d88.88.dP...`".`Ybo."
88""...88""...88"Yb..Yb...dP..8I..dY..8P...88.Y88..dP__Yb..88YbdP88.88.Yb......o.`Y8b
88.....888888.88..Yb..YbodP..8888Y"..dP....88..Y8.dP""""Yb.88.YY.88.88..YboodP.8bodP'


88b.88.888888.888888.Yb........dP.dP"Yb..88""Yb.88..dP
88Yb88.88__.....88....Yb..db..dP.dP...Yb.88__dP.88odP.
88.Y88.88"".....88.....YbdPYbdP..Yb...dP.88"Yb..88"Yb.
88..Y8.888888...88......YP..YP....YbodP..88..Yb.88..Yb

February 28, 2010

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:

function sidebar_container_widget(){
?>
<div id=”sidebar”>
<?php if ( function_exists(‘dynamic_sidebar’) ) { dynamic_sidebar(2); } ?>
</div>
<?php
}

function content_container_widget(){
?>
<div id=”content”>
<?php if ( function_exists(‘dynamic_sidebar’) ) { dynamic_sidebar(3); } ?>
</div>
<?php
}

function content_widget(){
?>
<!– Here you put your WordPress Loop–>
<?php
}

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’ => ‘<div>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ”,
‘after_title’ => ”,
));
register_sidebar(array(
‘name’ => ‘main content’,
‘before_widget’ => ‘<div>’,
‘after_widget’ => ‘</div>’,
‘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.

#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.

<div id=”header”>
<h1><a href=”<?php echo get_option(‘home’); ?>/”><?php bloginfo(‘name’); ?></a></h1>
<div><?php bloginfo(‘description’); ?></div>
</div>
<?php if ( function_exists(‘dynamic_sidebar’) ) { dynamic_sidebar(1); } ?>
<div id=”footer”>Footer Message</div>

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.

Converting Wordpress Themes To Habari
Habari timthumb?
one comment
page 694
Fun With Sidebar Tabs Styling
Tabs with html css on same page javascript?
2 comments
page 336
Using Your Own Url Shortener
How to have own url short?
4 comments
page 1190
Post Image The Easy Peasy Way
Insert conditional image php wp?
26 comments
page 1065
Using Your Own Url Shortener
Run short url using htaccess?
4 comments
page 1190
How To Add Sidebars To A Theme
Wordpress sidebar above main sidebars?
11 comments
page 1053
Html 5 Gallery
Html5 simple wordpress theme?
6 comments
page 1305
Dont Mess With My Toot Toot
New posttype not displayed wordpress?
16 comments
page 599
Quick N Dirty Replacement Text
Wpupdatepost object?
no comment
page 122
Updating Code Snippets Here
Fun wordpress plugin?
one comment
page 1338
How To Add Sidebars To A Theme
Wp register sidebars?
11 comments
page 1053
Html 5 Gallery
Html5 wordpress theme?
6 comments
page 1305
Post Image The Easy Peasy Way
Get post images?
26 comments
page 1065
Updating Code Snippets Here
Wordpress fun plugins?
one comment
page 1338
Post Image The Easy Peasy Way
Wordpress get first image large?
26 comments
page 1065
Using Wordpress As A Php Framework
Wordpress create your own framework?
2 comments
page 335
Improve Your Typography With Plugins
Wordpress typography plugin?
one comment
page 721
Quick N Dirty Replacement Text
Dirty replacement?
no comment
page 122
How To Add Sidebars To A Theme
How to add pages in footer wordpress?
11 comments
page 1053
Post Image The Easy Peasy Way
How to post all gallery images in one post wordpress?
26 comments
page 1065
Post Image The Easy Peasy Way
Get attachment by post?
26 comments
page 1065
Html 5 Gallery
Html 5 tab?
6 comments
page 1305
Wordpress 25 Exif Fields
Exif info display wordpress?
12 comments
page 230
Using Your Own Url Shortener
Tiny urls htaccess?
4 comments
page 1190
Six Million Ways To Die Choose One
6 million ways to die so i chose?
14 comments
page 1128
How To Add Sidebars To A Theme
Wp register sidebars?
11 comments
page 1053
Fun With Sidebar Tabs Styling
Funwithsidebartabs customize css?
2 comments
page 336
My Experience Of Flexx
Flexx theme review?
4 comments
page 1026
Wpunlimited The Ultimate Wordpress Theme
Ultimate admin theme?
3 comments
page 1141
Quick N Dirty Admin Login Screen
Css login screen?
no comment
page 128
Fun With Sidebar Tabs Styling
Css tabcontentcontainer?
2 comments
page 336
Html 5 Gallery
Html 5 e wordpress?
6 comments
page 1305
Dont Mess With My Toot Toot
Toot html5?
16 comments
page 599
  1 query every 1304 seconds, 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.