0

I have been struggling with this for hours now, any help appreciated. I am using Underscores theme with one sidebar (sidebar-1, on the left) and Custom Sidebars plugin so that I can have different side content on each page. The problem is when I add a new sidebar (sidebar-2) which I want to appear just above the footer to contain the same content on every page.

functions.php:

function myunderscores3_widgets_init() {
    register_sidebar( array(
        'name'          => esc_html__( 'Sidebar', 'myunderscores3' ),
        'id'            => 'sidebar-1',
        'description'   => esc_html__( 'Add widgets here.', 'myunderscores3' ),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h2 class="widget-title">',
        'after_title'   => '</h2>',
        //'before_title' => '<!--', 
        //'after_title' => '-->',       
    ) );
    register_sidebar( array(
        'name'          => esc_html__( 'Bottom Sidebar', 'myunderscores3' ),
        'id'            => 'sidebar-2',
        'description'   => esc_html__( 'Add widgets here.', 'myunderscores3' ),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h2 class="widget-title">',
        'after_title'   => '</h2>',
        //'before_title' => '<!--', 
        //'after_title' => '-->',       
    ) );
}
add_action( 'widgets_init', 'myunderscores3_widgets_init' );

sidebar.php (for the sidebar-1 content):

if ( ! is_active_sidebar( 'sidebar-1' ) ) {
    return;
}
?>

<aside id="secondary" class="widget-area">
    <?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside><!-- #secondary -->

page.php (to pull in sidebar-1):

<?php
get_sidebar();
?>
</div><!-- wrapper -->    

footer.php (to pull in sidebar-2):

if (is_active_sidebar( 'sidebar-2' )) {
      dynamic_sidebar( 'sidebar-2' );
  }
?>
    </div><!-- #content -->

After adding widget content to sidebar-2 this is working fine on every page apart from the one page that has no sidebar-1 content. On this page the sidebar-2 content is appearing twice - on the bottom as expected and in sidebar-1 content area also ! Furthermore the 'no-sidebar' class is missing from the body. In the backend the sidebar-1 widgets are located through each individual page whereas the sidebar-2 content is located through the widget ('As Sidebar for selected Post Types' - Pages). On the actual page where the sidebar-2 content is appearing twice I have not specified any sidebars inside the actual page as I am doing this through the widget settings. Any ideas ? Thank you

0

Template files are included, not called like a function. Even though you called get_sidebar(), behind the scenes that is eventually calling include. So in other words, your template pages (like footer.php, page.php, and sidebar.php) are not directly within a function, so you cannot 'return' out of them, as you are attempting to do in sidebar.php.

So WordPress is progressing past that point, and then running dynamic_sidebar( 'sidebar-1' );. Now here's where someone else will have to jump in and tell you why it's loading the wrong widgets. I didn't think it would fallback to an active sidebar, but at least based on your code it seems to be. WordPress definitely does that with menus unless you specifically tell it not to.

But in any case, I think if you just modify your code you'll avoid the behavior entirely:

if ( is_active_sidebar( 'sidebar-1' ) ) :
?>

<aside id="secondary" class="widget-area">
    <?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside><!-- #secondary -->

<?php endif; ?>

So all I did is use flip the check (removing the !) and then wrap the entire dynamic_sidebar section with it (using the alternate PHP format of : and endif;)

  • Thanks, it's very strange - with your code amendment even with the sidebar-2 code removed from footer.php the sidebar-2 content is still being pulled into the sidebar-1 area wrapped inside sidebar-1s <aside> tag. – rookieloops Apr 18 at 14:00
0

I have no idea what the problem was but after deleting all my code and creating the new side bar again it worked. maybe a typo somewhere or a caching issue ?

  • It does sound like a caching issue. – tmdesigned Apr 18 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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