WordPress.org

Ready to get started?Download WordPress

Forums

Using sidebar widgets in multiple sidebars (24 posts)

  1. blairm
    Member
    Posted 7 years ago #

    I'm having trouble getting sidebar widgets to work in multiple sidebars. I have widgets working correctly in one sidebar. I changed the code in functions.php to the following

    if ( function_exists('register_sidebars') )
    	register_sidebars(2, array('name'=>'Sidebar %d'));

    And I now see Sidebar 1 and Sidebar 2 in my admin screen. The first sidebar (which was already working) still shows up. But when I add the following code in the sidebar.php file, to call the second sidebar widget, both sidebars and the main content disappear:

    <?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar(1) ) : else : ?>

    Any ideas? I haven't been able to find any clear instructions for using widgets in multiple sidebars, I looked at the API documentation on the Automattic site, but I'm pretty new to PHP, so I may be missing something.
    Thanks in advance for any help!

  2. MichaelH
    Member
    Posted 7 years ago #

    At least I can point you to a theme that does do what you are trying...maybe something you can learn there.

    http://www.shamsulazhar.com/wp/archives/31

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

    What is the content of your existing sidebar.php file (just the first few widget related lines)? Where is the code that defines the second sidebar (not a widget sidebar, but the one you're replacing)?

    First off, you're going to use dynamic_sidebar(2) to get the second sidebar. Also, how you add it depends rather heavily on what you've already got there.

  4. blairm
    Member
    Posted 7 years ago #

    I have two different files for the sidebars- sidebar.php and sidebar2.php
    Sidebar2.php is the widget sidebar with the following code

    <?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar(1) ) : else : ?>

    It worked before and has continued to work after I changed the code in the function.php file.
    Sidebar.php defines the current, non-widget sidebar. It seems most two column themes have code for the sidebars all in one file. Would doing this help? I might give it a try.
    Thanks for your help, MichaelH and Otto42

  5. johnnyspade
    Member
    Posted 7 years ago #

    Admittedly, I'm not 100% sure what you're asking. Here's what I do for multiple sidebars, setting up separate sidebar.php files and calling them into the template You could have a dozen if you really wanted to. Here's an example for 4.

    Set up your functions.php file:

    <?php
    if ( function_exists('register_sidebar') )
    register_sidebar(array('name'=>'sidebar1',
    'before_widget' => '<div id="%1$s" class="side-c %2$s">', // Removes <li>
    'after_widget' => '</div>', // Removes </li>
    'before_title' => '<h3>', // Replaces <h2>
    'after_title' => '</h3>', // Replaces </h2>
    ));
    register_sidebar(array('name'=>'sidebar2',
    'before_widget' => '<div id="%1$s" class="side-c %2$s">', // Removes <li>
    'after_widget' => '</div>', // Removes </li>
    'before_title' => '<h3>', // Replaces <h2>
    'after_title' => '</h3>', // Replaces </h2>
    ));
    register_sidebar(array('name'=>'sidebar3',
    'before_widget' => '<div id="%1$s" class="side-c %2$s">', // Removes <li>
    'after_widget' => '</div>', // Removes </li>
    'before_title' => '<h3>', // Replaces <h2>
    'after_title' => '</h3>', // Replaces </h2>
    ));
    register_sidebar(array('name'=>'sidebar4',
    'before_widget' => '<div id="%1$s" class="side-c %2$s">', // Removes <li>
    'after_widget' => '</div>', // Removes </li>
    'before_title' => '<h3>', // Replaces <h2>
    'after_title' => '</h3>', // Replaces </h2>
    ));
    ?>

    Then, in each of the 4 sidebars, call the function using the convention as below:

    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar1') ) : ?>
    <?php endif; ?>
  6. blairm
    Member
    Posted 7 years ago #

    Thanks for the suggestion, Johnnyspade, yes I think you understand what I am trying to do (multiple sidebars that use sidebar widgets). However, I tried using the code you suggested and messed the files up again- everything disappeared except the header.
    I'm a little confused as to what the following code is for

    'before_widget' => '<div id="%1$s" class="side-c %2$s">', // Removes <li>
    'after_widget' => '</div>', // Removes </li>
    'before_title' => '<h3>', // Replaces <h2>
    'after_title' => '</h3>', // Replaces </h2>

    I don't have anything like that in the code I used to get the sidebar widgets working in the one sidebar I have where they do work. Is this part of the code necessary?
    Thanks again, everybody, for the help

  7. johnnyspade
    Member
    Posted 7 years ago #

    Hey blairm,

    Sorry for the late reply. You don't actually need that code. It's used to remove some of those tags in the setup I have it installed in. Sorry for making this more confusing for you. Remove those 4 lines from each register string, make sure you close the string properly, and when you check the widgets area under the presentation menu, you should have a seperate area for each of the specified pages.

    B/

  8. blairm
    Member
    Posted 7 years ago #

    Thanks for clearing that up, JohnnySpade. I left out the unnecessary code and just added the following code to functions.php

    <?php
    if ( function_exists('register_sidebar') )
    register_sidebar(array('name'=>'sidebar1',
    ));
    register_sidebar(array('name'=>'sidebar2',
    ));

    That gives me two sidebars under Sidebar Widgets in the presentation menu.
    Then when I add
    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar1') ) : ?>
    to the left sidebar it shows up correctly. If I leave
    <?php endif; ?> in, everything but the header disappears.
    Also, when I try to activate the second sidebar, using
    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar2') ) : ?>
    everything but the header disappears.
    Can anyone tell me why this second sidebar won't work?
    Thanks again , everybody, for the help.

  9. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    Can anyone tell me why this second sidebar won't work?

    Yeah. You're doing something wrong in your code.

    Without actually looking at your code, it's very difficult to explain what it is that you're doing wrong. I suggesting finding somebody to actually do it for you, or get a book on basic PHP skills and read it.

    Because when you say things like "If I leave <?php endif; ?> in, everything but the header disappears.", then it's clear that you have no idea what you're doing with regards to PHP, and so should probably go find a programmer to help you out. It's easy to do and only takes 5 minutes, but you do have to understand at least a small amount of programming. You can't just throw it all together and see what sticks.

  10. johnnyspade
    Member
    Posted 7 years ago #

    I'll agree with Otto42, you're probably moving into an area you're not ready for yet. You don't absolutely 'need' php knowledge but it's hard to push the system without it and it is difficult to tell you what's gone wrong without seeing the code. On the positive side, what you're doing is decent 'trial by fire' education. Nothing like buggering your work and figuring out how to fix it to shorten the learning curve.

    What you should look into further is using template tags to get different functionality for different pages without needing to implement conditional and if/else statements. Templates are your friend. More into here: http://codex.wordpress.org/Template_Tags

  11. rudolf45
    Member
    Posted 7 years ago #

    I can confirm the code provided by johnnyspade works nicely - thank you!
    I tried first time in my life to use widgets in two sidebars and it was quite easy, just had to copy the code. And it was clearly commented, so I understood even those additional lines.

  12. 3stripe
    Member
    Posted 7 years ago #

    Worked nicely for me as well, thanks!

    Now I just need to work out how to use a widget more than once (I want to have certain standard widgets in every sidebar)

  13. Ripsey
    Member
    Posted 7 years ago #

    Works great! Thanks a lot.

  14. marstonstudio
    Member
    Posted 7 years ago #

    I found this thread useful when implementing multiple sidebars. I wrote up some additional information, including how to have certain standard widgets in every sidebar and posted the info on my blog. Please forgive the shameless self promotion. :)

  15. dockarl
    Member
    Posted 6 years ago #

    marstonstudio - you deserve the shameless self promotion :-) That's a great write up.

    doc

  16. demonicume
    Member
    Posted 6 years ago #

    great write up.

    the wordpress community is a bunch of jerks. this is a hobby - not all of us code for Microsoft. we do this for fun. your cockiness hasnt been earned. i'm a music teacher. i'd be a jerk if every time some middle school kid asked me for help i told him to screw off and go read a book. 'go read a book and practice, practice, practice. it only takes 5 minutes. what the hell is wrong with with you? i could have played that lick in 5 minutes. youre obviously in way over your head. if you can't do this much, youre better off quiting

    my point being is that even if i read a dozen books on php, which i have and continue to do, some of these 'obvious' answers are rather difficult. so your sarcasm and smarminess is tacky and insulting. it reminds me to be more polite to the people in my own field who might lack my expertise. i'll end this rant by saying thanks to the people who gave me the hints it took to fix my code.

  17. Cephus
    Member
    Posted 6 years ago #

    I'm using multiple widget ready bars in my theme, check out the functions.php

    http://3dohio.com/wordpress/2007/08/14/wordpress-theme-black-and-tan/

  18. xavier039
    Member
    Posted 6 years ago #

    I have tried this script and wordpress registers 4 widgets but still only one sidebar. All 4 widgets are being placed in the same sidebar. What do I need to do so that my theme recognizes 4 different sidebars?

  19. greenpeas
    Member
    Posted 6 years ago #

    Hey Jon marstonstudio... your website's contact form isn't working!

    I have a couple sidebars working; but the formatting is off on the second. What could cause that?

    the site is here:
    http://fbcwaynesboro.org/wp - sidebar 2 on about page

  20. amijo79
    Member
    Posted 6 years ago #

    I can also confirm that johnnyspade's code worked great. Thanks for the info!

    And to Otto, your response was pretty harsh. These are "Support" forums right? The people who post questions are trying already; don't just tell them to try harder.

  21. itissue
    Member
    Posted 6 years ago #

    Is there a way to enable php code in text boxes provided by widgets? Also, when I used the categories widget, it made the first list item off to the top left of the rest of the items and it's not bulleted like the others. I find that strange.

    Cool! WordPress uses gravatars in their forums now!

  22. moshu
    Member
    Posted 6 years ago #

  23. itissue
    Member
    Posted 6 years ago #

    Awesome! Thanks moshu, that worked wonderfully!

  24. robd
    Member
    Posted 6 years ago #

    This should be marked as resolved. I've just carried out the steps outlined above by johnnyspade and it worked fine giving me two widgeted sidebars. Thank you.

Topic Closed

This topic has been closed to new replies.

About this Topic