Balkhis - Vision For Success

How to add a dynamic sidebar in Wordpress?

Dear Readers, I would like to thank you for reading this post. I highly value your feedback and would like to know more about my readers. Follow me on Twitter

Do you ever wonder that you have one sidebar, but you want to display widgets in other places, and they don’t come in with manual codes that you can enter in the page and avoid using the widgets. Well there are some widgets that doesn’t have the manual tags. The solution to those is to add more than one dynamic sidebar in wordpress and display them where you want. I like this because I can manage everything from my wordpress admin panel and have everything where I want on the template. Kill two birds with one stone.

I have made a detailed video, which I will recommend you to watch because that will make the steps much more understandable, but I think you need to visit here to actually get the codes because it might not be as clear in the video.

First of all, Open your functions.php which is located in the themes folder.

[raw]
<?php

if ( function_exists(‘register_sidebars’) )
register_sidebar();

?>

[/raw]

By default it shows something like I have put above. Replace these codes with the ones below:

[raw]

<?php
if ( function_exists(‘register_sidebars’) )
register_sidebar(‘2′);
register_sidebar(‘3′);
register_sidebar(‘4′);
?>
[/raw]

You can add the numbers as you go. Remember you start with 2 because sidebar 1 is already defined.

Now is the part where you need to see the video because that explains better with live tutorial.

You go to the admin panel, and get to the widgets page, and you will see more than one sidebar there. Upload the widgets in the desired sidebar. Say you want to display the mybloglog recent visitor widget in the footer. You copy that widget in your sidebar 2 or whichever one.

Than open the file where you want to display the codes. In our case footer.php

[raw]
<?php if ( function_exists(‘dynamic_sidebar’) && dynamic_sidebar(2) ) : else : ?>

<?php endif; ?>
[/raw]

Add the codes above, and make sure you change the sidebar number.

This should do it. Enjoy your dynamic sidebar. This will make your life so much comfortable now because you can edit settings through the admin panel rather than editing each plugin file individually.



To stay updated with the blog, please consider subscribing to my full feed RSS. You can also subscribe by Email and have new posts sent directly to your inbox. I hate spam as well, so I promise you that you will not be spammed.
Add to Social Bookmarks:
Add this Article to Digg Add this Article to Stumbleupon Add this Article to Del.icio.us Add this Article to Reddit Add this Article to Newsvine Add this Article to Technorati

RSS feed

37 Comments

Comment by Diane
2008-03-25 23:49:53

Hi Syed, great video but unfortunately no matter what I did I could not see exactly some of the important parts. I even headed over to YouTube to see if it was clearer. The video is larger, but still too blurry for me to see :(

That aside, I’m also wondering does this only work for WP 2.5?


Comment by Syed Balkhi
2008-03-26 02:02:10

sorry about the blur in the video, I guess next time I would just make the fixed size window and fit it all in there. No this will work in any verson if wordpress not just 2.5. I just showed 2.5 because that is the one I have on Balkhis.


 
Comment by Angad Sodhi
2008-06-16 02:18:02

You should really upgrade to WP 2.5
It is much much better!


 
 
Comment by Harry
2008-04-09 18:18:00

Nice one, this worked great your instructions were very helpful, now i can call different sidebars for different pages as i wanted to.
Diane it does work with all Wordpress inc. 2.5. It is not too hard, you can do it in 2 steps.
Step 1. Open functions.php and add the first section of code.
Step 2. Then open up the template page where you want the numbered sidebar to appear, e.g category.php and add the second part of code. Please give more details of what exactly you want for more specific help.

And Syed a problem is you have spaces in your php code between question mark and <.
Again Many Thanks.


Comment by Syed Balkhi
2008-04-09 18:56:23

Thanks Harry, it is fixed now.


 
 
Comment by David Cheong
2008-04-14 13:09:38

At that something new to me, and really straight to the point too. Thanks syed for the guiding video and word guide. :)


Comment by Syed Balkhi
2008-04-14 14:45:32

glad I am able to help you David :)


 
 
Comment by Quest For news
2008-04-14 23:11:07

That was helpful. I figured out how to add the mybloglog widget to a few of my blogs. :)


Comment by Syed Balkhi
2008-04-15 08:00:03

I am glad that I was able to help you a bit. I first used it on Balkhis due to that reason also, but then incorporated it everywhere just to make it more friendly with the admin panel.


 
 
Comment by thorri Subscribed to comments via email
2008-04-17 08:36:32

This was really helpful, thanks!

I added extra sidebars easily. After adding some content, including special php via “Executable PHP widget”, one of my dynamic sidebars includes a corrupt widget that I can’t remove (no “remove” button available – only error message).

Do you know how to “empty” a dynamic sidebar?


Comment by Syed Balkhi
2008-04-17 08:48:29

Umm you can just go to your admin panel. I am assuming that you are on the latest version 2.5

Go to Design > Widgets

On the right hand side it will show Current Widgets and below it there will be a select box saying Sidebar 1, Sidebar 2 and so on. Pick the sidebar where your widget is.

Click to edit your broken widget, and you should see the remove button there.

Hope this helped.


 
 
Comment by thorri Subscribed to comments via email
2008-04-17 10:40:28

Thanks for the reply. I wish it were so straight forward as you say.

The fact is that the top and bottom of the right hand widget is missing — so no “edit” and thus, no “remove”. Only the middle part shows — and in it is only an error message.


 
Comment by thorri Subscribed to comments via email
2008-04-17 10:42:28

Thanks for the reply. I am indeed using WP 2.5.

The fact is that the top and bottom of the right hand widget is missing — so no “edit” and thus, no “remove”. Only the middle part shows — and in it is only an error message.

I can probably just not use “sidebar 3″ which contains the error widget, but just knowing it’s there really annoys me. I wish I could make it go away.


Comment by Syed Balkhi
2008-04-17 11:38:38

edit your functions.php and you can get rid of the entire sidebar all together.


 
 
Comment by thorri Subscribed to comments via email
2008-04-17 17:11:41

I tried editing Sidebar 3 out of functions and then editing it in again. It went away, and came back when I edited it in again.

I think I have a fix though (through WordPress.org’s forums):
http://wordpress.org/support/topic/169863?replies=2


Comment by Syed Balkhi
2008-04-17 18:21:22

I am glad you got your answer.


 
 
Comment by soam
2008-05-31 00:19:46

Im not able to get my functions.php

How to do it??


 
Comment by Angad Sodhi
2008-06-16 02:19:47

Lovely post Syed, I had never heard of such a thing before. Sounds very logical and very useful. But I still like to edit the code itself. My sidebars generally have no widgets at all. I like to put everything in the codes


 
Comment by Rakhshinda
2008-06-18 14:45:47

Thanks for explaining i was trying to do it by myself once and it got real confusing, but i’ll watch this video and learn from it.


 
Comment by Enkay Blog
2008-06-18 17:22:41

Thanks for the great tutorial. I will have to try this out. I was going to ask you, how do you experiment with Wordpress, do you have a test blog set up because thats what I do.


 
 
2008-08-19 00:31:14

Thank you for the tips! It helped me a lot. Now I have two sidebars fully working in my website!!

You should try contribute to Wordpress Codex with this video. I haven’t found a clue on this issue in there.


Comment by Syed Balkhi
2008-08-19 00:58:05

I am glad you liked my tutorial. I made an account on codex and wrote on only one thread. I just can’t find time to go there. I wish I can make my own article saying how to make a dynamic sidebar and sticky it on WP Codecs. If you find people you are welcome to link them to this site.

:)


 
 
Comment by Eddie
2008-09-05 05:40:32

Thanks for the information. I could not find it anywhere else.
It Works great,I had a problem – when i say put in 234 it will show only 123 and never the sidebar I want to access, i.e if i input 2345678 it will only show 7 sidebars, and still i could not access the one that is really active.

so i looked at the source and sidebar code, still couldnt figure it out.

So what i did was delete it all and use your code, now I can see the active widgets displayed :)

All i wanna do now is take off the bullet points, any tips on that?

Thanks again for your help this saved me a lot of trouble.


Comment by Syed Balkhi
2008-09-05 05:46:58

Glad my codes were helpful. For your current problem:

Open your style.css

Look for your id sidebar. #sidebar

Then look for the #sidebar ul or something with your specific class.

Add this:

list-style: none;
list-style-type: none;

That should take care of the problem.

Let me know whatever the outcome is whether its fixed or not :)


Comment by Eddie
2008-09-05 06:36:34

That worked! Thanks. I just put {list-style: none;} right after #sidebar in style.css. I have lost the style of the default sidebar with the theme but thats something i should learn about anyway.

p.s a tutorial on importing linked graphics to the sidebar would be awesome ;)

thanky very much!

(Comments wont nest below this level)

 
 
 
Comment by Sydney Web Design
2008-09-26 00:03:12

Hi Thanks for the article, i’m making a few Wordpress pages and this has been an issue. good work
http://www.scottydonald.com


 
Comment by Gionata Subscribed to comments via email
2008-10-03 21:49:18

Dear Balkhis.

The code works great with me, but let’s say i want to display sidebar (2) in a page template, as a replacement of sidebar(1)?

How do you write the php code in the page template?

????

Thanks a lot.

Gionata Nencini
http://www.Partireper.it


Comment by Syed Balkhi
2008-10-03 23:21:23

Well in this case, you would have to place the sidebar code in the page rather than footer and modify the whole template. So basically what you will do is have the normal sidebar in the index.php, but when it is page.php insert the second sidebar …


Comment by Gionata Subscribed to comments via email
2008-10-03 23:26:54

thanks for the reply.

may i ask what is the code you would put in the page.php template to display the second sidebar?

I’ve tried few different combination, but it shows always the first one…

Thanks a lot.

Gionata Nencini
http://www.Partireper.it

(Comments wont nest below this level)

 
Comment by Gionata Subscribed to comments via email
2008-10-06 00:37:43

Dear Balkhi again…
Sorry for commenting again, but i really would like you to help me with that php code.
What do i write in page.php to acquire the second sidebar?

Regards.

Gionata Nencini
http://www.Partireper.it

(Comments wont nest below this level)

Comment by Syed Balkhi
2008-10-06 09:45:11

You change the number of the sidebar. If you would like me to do it please use the contact form … you can hire me to do it for you.


 
 
 
 
Comment by Abdel
2009-01-22 16:27:34

Thanks for the tip syed. I really like how you made it very straightforward. You saved my life and that of my actual theme cuz I was about to trash it.


 
Comment by KV Subscribed to comments via email
2009-03-20 14:36:02

Thanks for the video. How ever my dynamic side bar doesn’t work. This is what I think is the problem. My template came with a dynamic side bar.(I have gone back to the orginal sidebar) However, when I go to the widgets page on Wordpress only one sidebar is shown. And none of the widgets currently on my sidebar are shown. When I try to add widgets it deletes the orginal dymanic sidebar and replaces it with one. This is why I started searching for codes to fix or replace my tool bar which isn’t working properly. Please help if you can. I would really like to be able to add more widgets to my sidebar. Thanks.

To help I have added the codes in my template

THEME FUNCTIONS

SIDE BARS

Welcome to the Working Divas Blog!

STYLE SHEET
/*——————– Sidebar ——————–*/

#sidebar { float: right; width: 290px; margin: 0; padding: 0; }

#sidebar h2 { display: block; margin: 16px 0 6px 0; padding: 0 0 10px 3px; font-size: 1.4em; list-style-type: none; color: #CC0000; }

#sidebar ul { clear: both; margin: 0; padding: 0 0 20px 0; list-style-type: none; }
#sidebar ul li { clear: both; }

#sidebar ul.no-splitted li { background: transparent url(images/bullet.gif) no-repeat 5px 1px; padding: 0 0 0 23px; }
#sidebar ul.no-splitted li:hover { background: transparent url(images/bullet-blue.gif) no-repeat 5px 1px; padding: 0 0 0 23px; }
#sidebar ul.no-splitted li a:link, #sidebar ul.no-splitted li a:visited { color: #666; text-decoration: none; }
#sidebar ul.no-splitted li a:hover, #sidebar ul.no-splitted li a:active { color: #444; text-decoration: none; }

#sidebar ul.no-splitted ul li { margin: 0 -5px; }
#sidebar p, #sidebar form { margin: 0 5px; padding: 0 0 20px 0; }
#sidebar ul.blc { padding: 0 5px; }

#sidebar li#sidebar-splitted { float: left; width: 100%; margin: 0 0 15px 0; }
#sidebar-splitted .splitted-left { float: left; width: 140px; }
#sidebar-splitted .splitted-right { float: right; width: 145px; }

ul.splittedlists li { background: transparent url(images/bullet.gif) no-repeat 5px 1px; padding: 0 0 0 23px; }
ul.splittedlists li:hover { background: transparent url(images/bullet-blue.gif) no-repeat 5px 1px; padding: 0 0 0 23px; }
ul.splittedlists ul li { margin: 0 0 0 -23px; }

ul.splittedlists li a:link, ul.splittedlists li a:visited { color: #666; text-decoration: none; }
ul.splittedlists li a:hover, ul.splittedlists li a:active { color: #444; text-decoration: none; }


Comment by Syed Balkhi
2009-03-21 09:18:55

With what you are describing to me, I am going to say that your template did not come with a dynamic sidebar. It have the codes without the number. I recommend you follow the tutorial and add your dynamic sidebars.


 
 
2009-06-16 16:53:52

[...] How to add a dynamic sidebar in Wordpress? [...]


 
Comment by RJ Sherman
2009-07-17 11:21:01

When you are working in the new version of wordpress (2.8.1) you don’t do:

register_sidebar(1);
register_sidebar(2);
register_sidebar(3);

you do register_sidebars(3); instead and it knows to grab all three.

Good post – VERY helpful.


 

Sorry, the comment form is closed at this time.

Subscribe to Balkhis via RSS
Subscribe to Balkhis via Email