简体   繁体   中英

How to create a second sidebar in Underscores Wordpress theme?

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 similar problem 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.


These are the steps I did so far along with my code:

step 1:

register a new sidebar in functions.php , so right now the code for both of them looks like this:

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' );

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.

step 2:

create a sidebar-2.php file with 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 wherever I want the second sidebar to be displayed (eg into an index.php or single.php etc file):

<?php get_sidebar(2); ?>

Any ideas what I could've done wrong or what I'm missing?

<?php dynamic_sidebar( 'sidebar-2' ); ?> 

Will output the sidebar content. Have you configured the sidebars to contain widgets in the CMS?

Also would be worth making sure you are eitign the correct PHP template to output the sidebar.

You are most likely falling in the early escape condition.

is_active_sidebar() will return FALSE if you don't have any active widgets in the target sidebar.

Other than that the code is OK and should work.

As it always is, I happened to find what was wrong about a minute after posting this question (even though previously I've been wrapping my head around it for quite a bit).

It was one of those stupid tiny mistakes that break everything: I named the file with the new sidebar (sidebar-2.php) wrong (had an extra space at the end). Other than that, it turns out the code is okay (since it displayed the sidebar correctly right after I re-named the file).

I'm sorry to have taken your time, and hopefully, if someone ever needs to create a second sidebar within Underscores, this will help them in the future :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM