How to create a second sidebar in Underscores Wordpress theme?

admin

Administrator
Staff member
I have trouble creating and displaying a second sidebar in the Underscores (_s) Wordpress theme. I've searched the web through and through but didn't really find any suitable solution (even found <a href="https://stackoverflow.com/questions/19334974/underscores-wordpress-theme-adding-second-sidebar">a similar problem</a> here on StackOverflow, but the answer seems to be creating a conditional sidebar rather than a second standalone one).
I do know how to create sidebars in WordPress (even though I'm not that experienced yet), but this time it seems I'm missing something, because the sidebar just doesn't display on the website. I'd really appreciate it if someone could have a look at my code and point me in the right direction.
<hr />
These are the steps I did so far along with my code:
<h2>step 1:</h2>
register a new sidebar in
Code:
functions.php
, so right now the code for both of them looks like this:
Code:
function theme_name_widgets_init() {
    register_sidebar( array(
        'name'          =&gt; __( 'Sidebar', 'theme-name' ),
        'id'            =&gt; 'sidebar-1',
        'description'   =&gt; '',
        'before_widget' =&gt; '&lt;aside id=&quot;%1$s&quot; class=&quot;widget %2$s&quot;&gt;',
        'after_widget'  =&gt; '&lt;/aside&gt;',
        'before_title'  =&gt; '&lt;h1 class=&quot;widget-title&quot;&gt;',
        'after_title'   =&gt; '&lt;/h1&gt;',
    ) );
    register_sidebar( array(
        'name'          =&gt; __( 'Right Navigation', 'theme-name' ),
        'id'            =&gt; 'sidebar-2',
        'description'   =&gt; '',
        'before_widget' =&gt; '&lt;aside id=&quot;%1$s&quot; class=&quot;widget %2$s&quot;&gt;',
        'after_widget'  =&gt; '&lt;/aside&gt;',
        'before_title'  =&gt; '&lt;h1 class=&quot;widget-title&quot;&gt;',
        'after_title'   =&gt; '&lt;/h1&gt;',
    ) );
}
add_action( 'widgets_init', 'theme_name_widgets_init' );
It looks like this part isn't the problem, since the new sidebar appeared in the WordPress dashboard and I'm able to add widgets to it.
<h2>step 2:</h2>
create a
Code:
sidebar-2.php
file with the following code:
Code:
&lt;?php
if ( ! is_active_sidebar('sidebar-2') ) {
    return;
}
?&gt;

&lt;nav id=&quot;site-navigation&quot; class=&quot;main-navigation&quot; role=&quot;navigation&quot;&gt;
    &lt;?php dynamic_sidebar( 'sidebar-2' ); ?&gt;
&lt;/nav&gt;&lt;!-- #site-navigation --&gt;
<h2>step 3:</h2>
add this piece of code to wherever I want the second sidebar to be displayed (e.g. into an
Code:
index.php
or
Code:
single.php
etc file):
Code:
&lt;?php get_sidebar(2); ?&gt;
Any ideas what I could've done wrong or what I'm missing?