• 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!

Viewing 15 replies - 1 through 15 (of 23 total)
  • At least I can point you to a theme that does do what you are trying…maybe something you can learn there.

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

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    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.

    Thread Starter blairm

    (@blairm)

    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

    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; ?>
    Thread Starter blairm

    (@blairm)

    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

    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/

    Thread Starter blairm

    (@blairm)

    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.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    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.

    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: https://codex.www.ads-software.com/Template_Tags

    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.

    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)

    Works great! Thanks a lot.

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

    marstonstudio – you deserve the shameless self promotion ?? That’s a great write up.

    doc

    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.

Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘Using sidebar widgets in multiple sidebars’ is closed to new replies.