How to create a second sidebar in a Wordpress underline theme?

I'm having trouble creating and displaying a second sidebar in the Wordpress Substring (_s) Subject. I was browsing the web to the end, but I really could not find a suitable solution (I even found a similar problem here in StackOverflow, but the answer seems to create a conditional sidebar, not a second separate one).

I know how to create sidebars in WordPress (although I’m not so experienced yet), but this time it seems that I'm missing something because the sidebar just doesn’t appear on the website. I would really appreciate it if someone could take a look at my code and point me in the right direction.


These are the steps that I have done so far with my code:

step 1:

register the new sidebar in functions.php, so right now the code for them is as follows:

function theme_name_widgets_init() {
    register_sidebar( array(
        'name'          => __( 'Sidebar', 'theme-name' ),
        'id'            => 'sidebar-1',
        'description'   => '',
        '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'          => __( 'Right Navigation', 'theme-name' ),
        'id'            => 'sidebar-2',
        'description'   => '',
        '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', 'theme_name_widgets_init' );

This part does not seem to be a problem, since a new sidebar has appeared on the WordPress toolbar, and I can add widgets to it.

step 2:

create a file sidebar-2.phpwith the following code:

<?php
if ( ! is_active_sidebar('sidebar-2') ) {
    return;
}
?>

<nav id="site-navigation" class="main-navigation" role="navigation">
    <?php dynamic_sidebar( 'sidebar-2' ); ?>
</nav><!-- #site-navigation -->

Step 3:

add this piece of code to where I want the second sidebar to appear (for example, in a file index.phpor single.phpetc):

<?php get_sidebar(2); ?>

Any ideas what I could have done wrong or what I am missing?

+4
source share
3 answers
<?php dynamic_sidebar( 'sidebar-2' ); ?> 

Displays the contents of the sidebar. Have you configured sidebars to host widgets in the CMS?

, PHP- .

+1

, .

is_active_sidebar() FALSE, .

, .

+1

, , ( ).

, : (sidebar-2.php) ( ). , , ( ).

I'm sorry that you didn’t have the time and hopefully if someone needs to create a second sidebar in Underscores, this will help them in the future :)

+1
source

All Articles