1

Hello all I would like to add multiple sidebars to my wordpress site I currently have 2 sidebars registered in functions.php like so:

function themename_widgets_init() {
    register_sidebar( array(
        'name'          => __( 'Sidebar', 'themename' ),
        'id'            => 'sidebar-1',
        'description'   => 'Sidebar for all pages',
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget'  => '</aside>',
        'before_title'  => '<h1 class="widget-title">',
        'after_title'   => '</h1>',
    ) );

register_sidebar( array(
        'name'          => __( 'Sidebartwo', 'themename' ),
        'id'            => 'sidebar-2',
        'description'   => 'Sidebar for product page',
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget'  => '</aside>',
        'before_title'  => '<h1 class="widget-title">',
        'after_title'   => '</h1>',
    ) );



}
add_action( 'widgets_init', 'themename_widgets_init' );

in my sidebar.php I currently have this code which will add default data to my sidebars incase I have not added any widgets.

<div id="secondary" class="widget-area" role="complementary">
        <?php if ( ! dynamic_sidebar( 'sidebar-1' ) ) : ?>

            <aside id="search" class="widget widget_search">
                <?php get_search_form(); ?>
            </aside>

            <aside id="archives" class="widget">
                <h1 class="widget-title"><?php _e( 'Archives', 'themename' ); ?></h1>
                <ul>
                    <?php wp_get_archives( array( 'type' => 'monthly' ) ); ?>
                </ul>
            </aside>

            <aside id="meta" class="widget">
                <h1 class="widget-title"><?php _e( 'Meta', 'themename' ); ?></h1>
                <ul>
                    <?php wp_register(); ?>
                    <li><?php wp_loginout(); ?></li>
                    <?php wp_meta(); ?>
                </ul>
            </aside>

        <?php endif; // end sidebar widget area ?>
    </div><!-- #secondary -->

Would anyone know how to go about doing the same for my sidebar-2 from what I have read i must create a sidebar-2.php which I have done and included the same code as above like so:

            <aside id="search" class="widget widget_search">
                <?php get_search_form(); ?>
            </aside>

            <aside id="archives" class="widget">
                <h1 class="widget-title"><?php _e( 'Archives', 'themename' ); ?></h1>
                <ul>
                    <?php wp_get_archives( array( 'type' => 'monthly' ) ); ?>
                </ul>
            </aside>

            <aside id="meta" class="widget">
                <h1 class="widget-title"><?php _e( 'Meta', 'themename' ); ?></h1>
                <ul>
                    <?php wp_register(); ?>
                    <li><?php wp_loginout(); ?></li>
                    <?php wp_meta(); ?>
                </ul>
            </aside>

        <?php endif; // end sidebar widget area ?>
    </div><!-- #secondary -->

But unfortunately it is not working does anyone know why?

5
  • when you say it's not working, can you be more specific? are you seeing sidebar-1's content in both places, or nothing at all for sidebar-2? can you show the code from your theme template that is calling get_sidebar()? Feb 19, 2015 at 19:29
  • @karpstrucking I can see the widget areas in my dashboard and I can see the sidebar-1 showing up when I added the sidebar to my template page i use <?php get_sidebar('sidebar-2'); ?> the sidebar-1 still shows in its place.
    – steamfunk
    Feb 19, 2015 at 19:34
  • if your file is named sidebar-2.php then the correct use is get_sidebar( '2' ); Feb 19, 2015 at 19:35
  • lol @karpstrucking thanks alot that was the problem I thought it went <?php get_sidebar('ID'); ?> but its already registered and waiting to be called with a sidebar template page of course thank you very much again
    – steamfunk
    Feb 19, 2015 at 19:41
  • How would I mark this as resolved can you put your answer in an answer and not in a comment so you get the necc recognition. @karpstrucking
    – steamfunk
    Feb 19, 2015 at 19:44

1 Answer 1

0

If your sidebar file is named sidebar-1.php the correct usage of the get_sidebar() (WP Codex) function would be:

<?php get_sidebar( '1' ); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.